适配 iOS 13及以上系统

新系统iOS13一出,iOS开发者们都炸锅了,各种不适配和闪退,纷纷寻找更好的解决办法,下面是我总结的一些需要适配地方,希望对感兴趣的同学有所帮助,不足之处请多多指教,谢谢!

适配 iOS 13及以上系统

iOS 13

[UIApplication sharedApplication].keyWindow API将被弃用

@property(nullable, nonatomic,readonly) UIWindow *keyWindow API_DEPRECATED("Should not be used for applications that support multiple scenes as it returns a key window across all conneted scenes", ios(2.0, 13.0));

iOS 13 UIApplication keyWindow 被标记为API_DEPRECATED,

修改使用下方代码获取

[[[UIApplication sharedApplication] windows] objectAtIndex:0];

UISearchBar黑线处理导致崩溃

iOS 13之前为了处理搜索框的黑线问题,通常会遍历searchBar的subViews,找到并删除UISearchBarBackground。

修改为:

for (UIView *view in _searchBar.subviews.lastObject.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
view.layer.contents = nil;
break;
}
}

设置UISearchBarBackground 的layer.contents为nil。

UISearchDisplayController彻底弃用,导致崩溃

在iOS 8之前,我们在UITableView上添加搜索框需要使用UISearchBar + UISearchDisplayController 的组合方式。在iOS 13中,如果还继续使用UISearchDisplayController会直接导致崩溃,崩溃信息如下:

Terminating app due to uncaught exception 'NSGenericException', reason: 'UISearchDisplayController is no longer supported when linking against this version of iOS. Please migrate your application to UISearchController.'

解决方法:使用UISearchControllerr代替


私有API被封禁(KVC限制),禁止访问。

iOS 13中通过KVC方式来获取私有属性,有Crash风险,尽量避免使用。比如我们续航用的UITextField和UISearchController等,在iOS 13的searchBar添加一个

- (void)set_cancelButtonText:(NSString *)text

方法,专门用来命中kvc,一旦命中就Crash。

//修改textField的占位符字体颜色
[textField setValue:[UIColor xxx] forKeyPath:@"_placeholderLabel.textColor"];

1、获取SearchBar的textField

由于在iOS13中把SearchBar中的textField直接暴露给开发者使用,无需再通过kvc获取

#ifdef __IPHONE_13_0
if (@available(iOS 13.0, *)) {
UITextField *searchField = self.searchTextField;
}else {
UITextField *searchField = [self valueForKey:@"_searchField"];
}
#endif

#endif

2、修改TextField的占位符字体大小以及颜色,在iOS13中不能通过KVC来进行修改,可以通过其属性字符串来修改

UITextField *textfield = [[UITextField alloc]init];
NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc]
initWithString:textfield.placeholder
attributes:@{NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName:[UIFont systemFontOfSize:12]}
];
textfield.attributedPlaceholder = arrStr;

获取SearchBar的cancelButton,由于searchBar的层级发生变化以及对象的局部变量,因为无法通过KVC的方式来获取

if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 13.0) {
for(id cc in [self.searchBar subviews]) {
for (id zz in [cc subviews]) {
for (id gg in [zz subviews]) {
if([gg isKindOfClass:[UIButton class]]){
UIButton *cancelButton = (UIButton *)gg;
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
}else{
UIButton*cancelButton = (UIButton *)[self.searchBar getVarWithName:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}

MPMoviePlayerController在iOS13中废弃

MPMoviePlayerController is no longer available.Use AVPlayerViewController in AVKit.在iOS 13中对于MPMoviePlayerController使用的废弃,需要使用AVKit中的AVPlayerViewController来达到播放的目的。


Sign in with Apple 第三方登录

当 Sign in with Apple 服务正式上线以后,所有已接入其它第三方登录的App,Sign in with Apple 将要被要求作为一种登录选择,否则有可能就不给过。如果App支持三方登录(Google、FaceBook、微信、QQ、支付宝等),就必须支持苹果登录,且要放前面。

解决办法:未来上线之前,添加登录入口即可。


即将废弃的LaunchImage

从iOS 8的时候,苹果就引入了LauchScreen,我们可以设置LaunchScreen来作为启动页。当然,现在你还可以使用LaunchImage来设置启动图。不过使用LaunchImage的话,要求我们必须提供各种屏幕尺寸的启动图,来适配各种设备,随着苹果设备尺寸越来越多,这种方式显然不够Flexible。而使用LaunchScreen的话,情况会变得很简单,LaunchScreen是支持AutoLayout+SizeClass的,所以适配各种屏幕不在话下。

从2020年4月开始,所有使用iOS 13SDK的App将必须提供LaunchScreen,LauchImage即将推出历史舞台。可以使用Launch Storyboards来进行解决。


模态弹出默认交互改变

iOS 13的presentViewController默认有视差效果,模态出来的界面现在默认都下滑返回。一些页面必须要点确认才能消失,需要适配。如果项目中页面高度全部是屏幕尺寸,那么多出来的高度就会出现问题。

// Objective-Cself.modalPresentationStyle = UIModalPresentationFullScreen;

UIViewController 增加一个了属性 isModalInPresentation,默认为 false,当该属性为 false 时,用户下拉可以 dismiss 控制器,为 true 时,下拉不可以 dismiss控制器。


UISegmentControl默认样式改变

默认样式变味白底黑字,如果设置修改过颜色的话,页面需要修改


黑夜模式

适配 iOS 13及以上系统

暗黑模式

Apps on iOS 13 are expected to support dark mode Use system colors and materials Create your own dynamic colors and images Leverage flexible infrastructure

审核强制要求适配黑夜模式。

// 模式强制切换
if (darkMode) {
if (@available(iOS 13.0, *)) {
[UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
}
} else {
if (@available(iOS 13.0, *)) {
[UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
}

全局关闭黑暗模式

方式一:配置plist文件:在info.plist文件中,添加UIUserInterfaceStyle key名字为User Interface Style 值为String,将UIUserInterfaceStyle key对应的值设置为Light在开发中,如果用系统组件(如cell、tableView的背景色)为这支背景色(或者为透明),则进入暗黑模式后,控件背景色变为黑色。

可以每一个页面设置,当然也可以整体设置,一般我们的App都是在window下的,那就整体设置App里的window。

方式二:代码关闭黑暗模式 强制关闭暗黑模式

#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if(@available(iOS 13.0,*)){

self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
#endif

单个界面不遵循暗黑模式

UIViewController与UIView有新增一个属性 overrideUserInterfaceStyle

将overrideUserInterfaceStyle设置为对应的模式,则强制限制该元素与其子元素以设置的模式进行展示,不跟随系统模式改变进行改变

1、设置ViewController的该属性,将会影响视图控制器的视图和自视图控制器采用该样式2、设置view的该属性,将会影响师徒及自视图采用该属性3、设置Window的该属性,将会影响窗口中的所有内容都采用该样式,包括根视图控制器和在该窗口中显示内容的所有演示控制器(UIPresentationController)


UIScrollView滚动条异常偏移

屏幕旋转可能会触发系统对滚动条的自动修正如果没有修改需求,挂壁该特性即可

#ifdef __IPHONE_13_0
if (@available(iOS 13.0, *)) {
self.automaticallyAdjustsScrollIndicatorInsets = NO;
}
#endif

UICollectionView异常API

该API在iOS 13下回强制将cell置中,导致上部留白。

推测:底部应该也会有留白的情况。

#pragma mark - 修复iOS13 下滚动异常API
#ifdef __IPHONE_13_0
- (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)];
}
}
#endif

WKWebView暗黑适配

要点:1、模式参数通过 UA 传递2、联调中出现加载闪白问题

// 解决办法:设置webview的opaque属性为false
webView.opaque = false;
适配 iOS 13及以上系统

iPhone 11


分享到:


相關文章: