iOS13 适配问题 看这一篇就够了

技术参考:

apple login

IOS13适配-详细

iOS 13 适配(持续更新中)

iOS13适配

掘金 iOS 13适配01

掘金 适配 iOS13

iOS 13 更新tips

View Controller Presentation Changes in iOS 13

1. Apple Login (2020年4月份)

附上官方Demo:下载地址

2. Dark Mode

2.1 暗黑模式 状态判断 && 切换监听

  • 判断模式状态
if (UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
    // 暗黑模式
}
else {
    // 正常模式
}

  • 监听模式切换
// 注意:参数为变化前的traitCollection
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection;

// 判断两个UITraitCollection对象是否不同
- (BOOL)hasDifferentColorAppearanceComparedToTraitCollection:(UITraitCollection *)traitCollection;

2.2 禁用暗黑模式

  • info.plist 内APP级别禁用暗黑模式

    Key: User Interface Style
    value: Light

  • widow级别的禁用暗黑模式

    if (@available(iOS 13.0, *)) {
        [UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    
    
  • ViewController级别禁用暗黑模式

    想全局禁用需要在 baseVC内全局禁用

    #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    - (UIUserInterfaceStyle)overrideUserInterfaceStyle{
        return UIUserInterfaceStyleLight;
    }
    #endif
    
    
  • view级别禁用暗黑模式

    view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    
    

2.3 适配暗黑模式颜色

参考这里 :iOS开发如何适配暗黑模式(Dark Mode)

+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

[UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trait) {
    if (trait.userInterfaceStyle == UIUserInterfaceStyleDark) {
        return UIColorRGB(0x000000);
    } else {
        return UIColorRGB(0xFFFFFF);
    }
 }];

3. present 半屏问题

modalPresentationStyle属性默认不是UIModalPresentationFullScreen了,需要根据需求手动设置。

LXNavigationViewController *nav = [[LXNavigationViewController alloc] initWithRootViewController:loginViewController];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nav animated:YES completion:nil];

  • 全局hook presentViewController方法

    + (void)hoookPresentFuc{
        [UIViewController aspect_hookSelector:@selector(presentViewController:animated:completion:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo){
            UIViewController *presentingVC =  (UIViewController *)aspectInfo.arguments.firstObject;
            presentingVC.modalPresentationStyle = UIModalPresentationFullScreen;
        } error:NULL];
    }
    
    

4. UITextField

  • 支付输入密码有问题

UITextField 的私有属性 _placeholderLabel 被禁止访问了

  • _placeholderLabel.textColor 私有属性被禁止访问
[self.textField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

崩溃信息:

'Access to UITextField's _placeholderLabel ivar is prohibited. 
This is an application bug' 

  • 解决方案
UITextField有个attributedPlaceholder的属性,我们可以自定义这个富文本来达到我们需要的结果。

NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : self.placeholderColor}];
_textField.attributedPlaceholder = placeholderString;

  • 项目适配代码
if (@available(iOS 13.0, *)) {
    if (textField.attributedText.length > 0) {
        color = textField.attributedText.color;
    }
}else{
    color = [textField valueForKeyPath:@"_placeholderLabel.textColor"];
}

iOS 13 通过 KVC 方式修改私有属性,有 Crash 风险,谨慎使用!并不是所有KVC都会Crash,要尝试!

5. UITextView 同上

6. navigationBar 使用UINavigation+SXFixSpace

if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
    // 结构调整后 在这里return掉
    if (@available(iOS 13.0, *)) {
        if ([NSStringFromClass(subview.class) containsString:@"_UINavigationBarContentView"]) {
            return;
        }
    }

    //可修正iOS11之后的偏移
    subview.layoutMargins = UIEdgeInsetsMake(0, space, 0, space);break;
}

同时,下面的属性也已经被禁止访问了:

  • [barBgView valueForKey:@"_shadowView"];

  • [barBgView valueForKey:@"_backgroundEffectView"];

7. statusBar

  • UIStatusBarStyleDefault枚举值代表含义有变化

    typedef NS_ENUM(NSInteger, UIStatusBarStyle) {
    UIStatusBarStyleDefault                                  = 0, // 根据用户交互样式自动选择状态条样式
    UIStatusBarStyleLightContent     API_AVAILABLE(ios(7.0)) = 1, // Light content, for use on dark backgrounds
    UIStatusBarStyleDarkContent     API_AVAILABLE(ios(13.0)) = 3, // Dark content, for use on light backgrounds
    UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,
    UIStatusBarStyleBlackOpaque      NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
    } API_UNAVAILABLE(tvos);
    
    
  • 定制状态条的样式 使用apple 推荐的这个系统方法

// ios 13.0 之后,这个方法已经失效了
// [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)){
        return UIStatusBarStyleDarkContent;
    }
    return UIStatusBarStyleDefault;
}

或者 ------>>:

UIStatusBarStyleDefault 替换为

(@available(iOS 13.0, *) ? UIStatusBarStyleDarkContent : UIStatusBarStyleDefault)

如果你还是使用的Xcode 10 为了走过编译器这一步,可以使用预编译命令,这么写:

#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    if (@available(iOS 13.0, *)) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDarkContent];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
#else
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
#endif

8. UITabbar

  • 横线
    UITabbar 层次发生改变,无法通过设置 shadowImage去掉上面的线;
  • 红点
    如果之前有通过TabBar上图片位置来设置红点位置,在iOS13上会发现显示位置都在最左边去了。遍历UITabBarButton的subViews发现只有在TabBar选中状态下才能取到UITabBarSwappableImageView,解决办法是修改为通过UITabBarButton的位置来设置红点的frame

9. UISearchBar

  • UISearchBar的私有属性UISearchBarBackground,禁止访问和remove
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
    if (@available(iOS 13.0, *)) {
        subview.backgroundColor = [UIColor lightGrayColor];
        // subview.layer.contents = nil;
    }else{
        [subview removeFromSuperview];
    }
}

10. 增加一直使用蓝牙的权限申请

info.plist文件下
Key: NSBluetoothAlwaysUsageDescription
Value: 我们要一直使用您的蓝牙,具体做什么别问我

11. 推送 获取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);
}

12. CNCopyCurrentNetworkInfo

 An app that fails to meet any of the above requirements receives the following return value:

- An app linked against iOS 12 or earlier receives a dictionary with pseudo-values. In this case, the SSID is Wi-Fi (or WLAN in the China region), and the BSSID is 00:00:00:00:00:00.
- An app linked against iOS 13 or later receives NULL.
- 

iOS13 以后只有开启了 Access WiFi Information capability,才能获取到 SSID 和 BSSID

参考:CNCopyCurrentNetworkInfo

13. APP 启动速度优化 导致frame获取失败

App启动过程中,部分View可能无法实时获取到frame。

14. WKWebView 中测量页面内容高度的方式变更

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

15. fishhook 导致的Crash

github fishhook

16. MPMoviePlayerController 已经被弃用

'MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.' 

17. LaunchImage 即将消失

从 iOS 8 的时候,苹果就引入了 LaunchScreen,我们可以设置 LaunchScreen来作为启动页。当然,现在你还可以使用LaunchImage来设置启动图。不过使用LaunchImage的话,要求我们必须提供各种屏幕尺寸的启动图,来适配各种设备,随着苹果设备尺寸越来越多,这种方式显然不够 Flexible。而使用 LaunchScreen的话,情况会变的很简单, LaunchScreen是支持AutoLayout+SizeClass的,所以适配各种屏幕都不在话下。
注意啦⚠️,从2020年4月开始,所有使⽤ iOS13 SDK 的 App 将必须提供 LaunchScreen,LaunchImage即将退出历史舞台。

18. window层级变化

遍历视图获取 keywindow 的方式会失效。
新增了 screenWindow 自定义视图和 keywindow之间新增iOS 13系统的特有图层。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • 原文博客地址: iOS13适配深色模式(Dark Mode) 好像大概也许是一年前, Mac OS系统发布了深色模...
    TitanCoder阅读 36,665评论 8 61
  • 1.私有KVC [self setValue:baseTabBar forKey:@"tabBar"]; //正常...
    小和大大阅读 774评论 0 0
  • 随着iPhone 11的发布,iOS 13适配也提上了日程,刚好最近在做项目适配,顺便总结一下:首先升级Xcode...
    二猪哥阅读 6,160评论 2 36
  • 1.私有KVC 在Xcode10上编译不会有问题,但是在Xcode11上编译的会崩溃。并且- (void)setV...
    iOS亮子阅读 906评论 0 2
  • 1. KVC调用私有方法崩溃 在Xcode10上编译不会有问题,但是在Xcode11上编译的会崩溃。并且- (vo...
    chasitu阅读 1,587评论 0 2