適配 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


分享到:


相關文章: