iOS 关于iOS13那些事

本文记录一些关于iOS 13的内容,欢迎指正和补充!

一、关于iOS13适配

  • 1.关于一些私有属性的适配,iOS 13出来限制了对私有属性的访问,会造成Crash,举个例子:
下面这段代码在iOS13以前可以这样写,没问题,但是在iOS 13以后就会Crash!
// 使用的私有方法
[_textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
// 崩溃提示信息
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug' 
修改如下:
// 替换的方案
_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"占位符文字"attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}]; 
这些也是私有方法,也会crash
- UITabBarButton -> _info
- UITextField -> _placeholderLabel
- _UIBarBackground -> _shadowView
- _UIBarBackground -> _backgroundEffectView
- UISearchBar -> _cancelButtonText
- UISearchBar -> _cancelButton
- UISearchBar -> _searchField
  • 2.关于present出来的页面留有空隙 可以写个分类 看这个
//在present之前:
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:animated completion:completion];
//下拉页面的时候,页面会消失,不希望这样的话,
vc. isModalInPresentation = YES //默认为NO
  • 3.关于一些废弃
    1.MPMoviePlayerController在iOS13中废弃 2.废弃 UISearchDisplayController 3.废弃UIWebView(必须,有可能被拒!)
  • 4.Sign in with Apple 第三方登录

当 Sign In with Apple 服务正式上线以后,如果你的APP支持第三方登录(Facbook、Google、微信、QQ、支付宝等)就必须支持苹果登录,否则有可能审核被拒。

  • 5.UISearchBar 黑线处理导致崩溃
for (UIView *view in _searchBar.subviews.lastObject.subviews) {
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        view.layer.contents = nil;
        break;
    }
} 
  • 6.[UIApplication sharedApplication].keyWindow API将被弃用.替换如下:
[[[UIApplication sharedApplication] windows] objectAtIndex:0]
  • 6.UICollectionView 异常API, 解决:
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated{
    [super scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
    //修复13下 Cell滚动位置异常
    //顶部
    if(self.contentOffset.y < 0){
        [self setContentOffset:CGPointZero];
        return;
    }
    //底部
    if(self.contentOffset.y > self.contentSize.height){
        [self setContentOffset:CGPointMake(0, self.contentSize.height)];
    }
}
  • 7.WKWebView 中测量页面内容高度的方式变更如下:

iOS 13以前 document.body.scrollHeight iOS 13中 document.documentElement.scrollHeight 两者相差55 应该是浏览器定义高度变了

  • 8.推送的 deviceToken 获取到的格式发生变化:
#include <arpa/inet.h>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = [deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    NSLog(@"deviceToken:%@", hexToken);
}

二、暗黑模式适配:

这个暗黑模式适配主要是重2方面下手,第一个方面是'图片',另一方面就是'颜色'

  • 首先我们先来看图片是如何适配的:


    WechatIMG5.jpeg
就是需要你有2套图片,一套是正常显示的,另一套就是暗黑显示的,系统会自动根据当前模式显示哪个图片!
  • 我们再来看颜色是如何适配的,iOS 13在UIColor中多了一些方法,颜色就是根据这些方法动态设置的,在 iOS 13 中UIView 和 UIViewController 、UIScreen、UIWindow 都已经遵从了UITraitEnvironment 这个协议,因此这些类都拥有一个叫做 traitCollection 的属性,在这些类中,我们可以这样去判断当前 App 的颜色模式,然后进行颜色设置,用系统的API设置颜色:
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider;
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider;
这样一来我们可以给UIColor写一个分类,实现对颜色的适配如下:
#import "UIColor+ZW.h"

@implementation UIColor (ZW)

+(UIColor *)ZW_settingDynamicColorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor{
    if (@available(iOS 13.0, *)) {
        UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return lightColor;
            }else {
                return darkColor;
            }
        }];
        return dyColor;
    }else{
        return lightColor;
    }
}

@end
  • 使用与调试:
使用:
//如果是暗黑模式的话文字显示白色,正常的话就显示darkGrayColor
UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(100, 240, 100, 30);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"适配暗黑模式";
    label.adjustsFontSizeToFitWidth = YES;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor ZW_settingDynamicColorWithLightColor:[UIColor darkGrayColor] darkColor:[UIColor whiteColor]];
    [self.view addSubview:label];
调试:
WechatIMG6.jpeg
这样一来,暗黑模式就适配完事了,是不是很简单呀!
代码关闭黑暗模式 强制关闭暗黑模式,在Appdelegate设置:
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if(@available(iOS 13.0,*)){
    self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
#endif

三、SF Symbols 内置图标库

感觉这个东西挺好玩的,哈哈哈,官方文档链接点击可以看一下并下载SF Symbols,感觉就是一个内置的图标库,如下图:
WechatIMG7.jpeg
那代码如何使用和实现呢?当然是很简单的啦,如下:
if (@available(iOS 13.0, *)) {
        imageV.image = [UIImage systemImageNamed:@"trash"];
    }
    else
    {
        imageV.image = [UIImage imageNamed:@"testD"];
    }
//这样就完事了,显示出来了,当然还有一些其他设置的属性,可以自己点击去查看

持续更新中......

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容