本文主要分享一下 iOS15 上適配方案,僅做開發記錄使用,開發過程中通過使用陸續增加。
iOS15 的適配,很重要的一環就集中在UINavigationBar和UITabbar方面。
用新Xcode13編譯工程后,iOS15項目顯示出現視圖問題。
iOS15 適配
1、UINavigationBar
2、UITabBar
3、TableView
4、Image
? UINavigationBar
? 從 iOS 15 開始,UINavigationBar在控制器中關聯滾動視圖頂部使用;
在iOS15中,UINavigationBar默認是透明的,有滑動時會逐漸變為模糊效果,可以通過改變UINavigationBar.scrollEdgeAppearance屬性直接變為模糊效果、配置相關屬性-背景、字體等
現有問題:
用新Xcode13編譯iOS15項目后,導航欄的問題比較明顯,調試之后發現是UINavigationBar部分屬性的設置在iOS15上是無效的。運行起來后發現,導航欄顏色設置失效,字體顏色也失效,并且有導航欄陰影黑線。
查看導航欄的相關API:
UINavigationBarAppearance
后發現,iOS15navigationBar的相關屬性設置要通過實例UINavigationBarAppearance來實現,UINavigationBarAppearance是iOS13更新的API,應該有人已經在用,我們的應用兼容iOS10+,對于導航欄的設置還沒有使用UINavigationBarAppearance,如今在iOS15上失效,所以對于呈現的問題,做如下適配:
// 修改NarBar背景
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
// 背景色
appearance.backgroundColor = [UIColor blueColor];
// 去掉半透明效果
appearance.backgroundEffect = nil;
// 標題字體顏色及大小
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
};
// 設置導航欄下邊界分割線透明
appearance.shadowImage = [[UIImage alloc] init];
// 去除導航欄陰影(如果不設置clear,導航欄底下會有一條陰影線)
appearance.shadowColor = [UIColor clearColor];
// standardAppearance:常規狀態, 標準外觀,iOS15之后不設置的時候,導航欄背景透明
self.navigationBar.standardAppearance = appearance;
// scrollEdgeAppearance:被scrollview向下拉的狀態, 滾動時外觀,不設置的時候,使用標準外觀
self.navigationBar.scrollEdgeAppearance = appearance;
}
? UITabBar
? 從 iOS 15 開始, UITabBar 在控制器中關聯滾動視圖底部時使用UITabBarAppearance.scrollEdgeAppearance配置相關屬性-背景、字體等
現有問題:
用新Xcode13編譯iOS15項目后,tabbar的問題和navigationBar的問題屬于類類似,運行起來后發現,tabbar背景顏色設置失效,字體顏色也失效,并且陰影設置也失效。
可查看TabBar的相關API:
UITabBarAppearance
后發現,iOS15的tabBar的相關屬性設置要通過實例UITabBarAppearance來設置,所以對于呈現的問題,做如下適配:
// 修改tabbar背景
if (@available(iOS 15.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
//tabBar背景顏色
appearance.backgroundColor = [UIColor whiteColor];
// 去掉半透明效果
appearance.backgroundEffect = nil;
// tabBaritem title選中狀態顏色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{
NSForegroundColorAttributeName:KColorFromRGB(0x53A2F8),
NSFontAttributeName:[UIFont systemFontOfSize:12],
};
//tabBaritem title未選中狀態顏色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{
NSForegroundColorAttributeName:KColorFromRGB(0x7E7E7E),
NSFontAttributeName:[UIFont systemFontOfSize:12],
};
self.tabBar.scrollEdgeAppearance = appearance;
self.tabBar.standardAppearance = appearance;
}
? TableView
? 從 iOS 15 開始,TableView 增加sectionHeaderTopPadding
屬性,默認情況sectionHeaderTopPadding會有22個像素的高度,及默認情況,TableView section header增加22像素的高度
可做如下適配:
if (@available(iOS 15.0, *)) {
self.tableView.sectionHeaderTopPadding = 0;
}
? Image
? 在iOS15中,UIImageWriteToSavedPhotosAlbum存儲圖片之后的回調不再返回圖片了,會返回nil,如果在回調方法里面操作image有可能會直接Crash,目前的解決辦法聲明一個全局image去記錄,后面再去操作:
self.image = savedImage;
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
// self.image doing...
}