對(duì)于iOS15適配匯總以及遇到的問(wèn)題
注意:以下適配內(nèi)容,必須適配的會(huì)以"必須"標(biāo)出
UITableView Section的header增加默認(rèn)間距
if (@available(iOS 15.0, *)) {
self.tableView.sectionHeaderTopPadding = 0;
}
導(dǎo)航欄異樣
iOS 15中,導(dǎo)航欄的問(wèn)題比較明顯,調(diào)試之后發(fā)現(xiàn)是UINavigationBar部分屬性的設(shè)置在iOS 15上是無(wú)效的,查看導(dǎo)航欄特性API,蘋(píng)果對(duì)導(dǎo)航欄的性能做了優(yōu)化,默認(rèn)情況下,如果導(dǎo)航欄與視圖沒(méi)有折疊,導(dǎo)航欄的背景透明,如果系統(tǒng)檢測(cè)到有重疊的話,會(huì)變成毛玻璃的效果。
注意??:UINavigationBarAppearance是iOS 13更新的API,iOS 15 navigationBar的相關(guān)屬性設(shè)置要通過(guò)實(shí)例UINavigationBarAppearance來(lái)實(shí)現(xiàn)。
在iOS 13 UINavigationBar新增了scrollEdgeAppearance屬性,但在iOS 14及更早的版本中此屬性只應(yīng)用在大標(biāo)題導(dǎo)航欄上。在iOS 15中此屬性適用于所有導(dǎo)航欄。
對(duì)于scrollEdgeAppearance屬性的說(shuō)明:
When a navigation controller contains a navigation bar and a scroll view, part of the scroll view’s content appears underneath the navigation bar. If the edge of the scrolled content reaches that bar, UIKit applies the appearance settings in this property.
If the value of this property is nil, UIKit uses the settings found in the standard Appearance property, modified to use a transparent background. If no navigation controller manages your navigation bar, UIKit ignores this property and uses the standard appearance of the navigation bar.
//舊代碼
- (void)customizeInterface {
//設(shè)置Nav的背景色和title色
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
[navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
[navigationBarAppearance setShadowImage:[UIImage new]];
[navigationBarAppearance setTintColor:[UIColor colorWithHexString:@"333333"]];//返回按鈕的箭頭顏色
NSDictionary *textAttributes = @{
NSFontAttributeName:[UIFont fontWithName:@"SourceHanSerifSC-Bold" size:18.0],
NSForegroundColorAttributeName: [UIColor colorWithHexString:@"333333"],
};
[navigationBarAppearance setTitleTextAttributes:textAttributes];
navigationBarAppearance.backIndicatorImage = [UIImage imageNamed:@"image_common_navBackBlack"];
navigationBarAppearance.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"image_common_navBackBlack"];
[[UITextField appearance] setTintColor:kColorLightBlue];//設(shè)置UITextField的光標(biāo)顏色
[[UITextView appearance] setTintColor:kColorLightBlue];//設(shè)置UITextView的光標(biāo)顏色
[[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarPosition:0 barMetrics:UIBarMetricsDefault];
}
//新代碼
- (void)customizeInterface {
//設(shè)置Nav的背景色和title色
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
[appearance setBackgroundColor:[UIColor whiteColor]];
[appearance setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
[appearance setShadowImage:[UIImage imageWithColor:[UIColor whiteColor]]];
appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightSemibold],NSForegroundColorAttributeName: [UIColor colorWithHexString:@"333333"]};
[appearance setBackIndicatorImage:[UIImage imageNamed:@"image_common_navBackBlack"] transitionMaskImage:[UIImage imageNamed:@"image_common_navBackBlack"]];
[[UINavigationBar appearance] setScrollEdgeAppearance: appearance];
[[UINavigationBar appearance] setStandardAppearance:appearance];
}
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
[navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
[navigationBarAppearance setShadowImage:[UIImage new]];
[navigationBarAppearance setTintColor:[UIColor colorWithHexString:@"333333"]];
[navigationBarAppearance setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f weight:UIFontWeightSemibold],NSForegroundColorAttributeName: [UIColor colorWithHexString:@"333333"]}];
navigationBarAppearance.backIndicatorImage = [UIImage imageNamed:@"image_common_navBackBlack"];
navigationBarAppearance.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"image_common_navBackBlack"];
[[UITextField appearance] setTintColor:[UIColor colorWithHexString:System_color]];
[[UITextView appearance] setTintColor:[UIColor colorWithHexString:System_color]];
[[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarPosition:0 barMetrics:UIBarMetricsDefault];
}
UITabbar
tabbar和navigationBar的問(wèn)題屬于同一類(lèi),tabbar背景顏色設(shè)置失效,字體設(shè)置失效,陰影設(shè)置失效問(wèn)題。
//舊代碼
self.tabBar.backgroundImage = UIColor.white.image
self.tabBar.shadowImage = UIColor.init(0xEEEEEE).image
item.setTitleTextAttributes(norTitleAttr, for: .normal)
item.setTitleTextAttributes(selTitleAttr, for: .selected)
注意??:首先是背景色設(shè)置失效,需要用UITabBarAppearance來(lái)設(shè)置
//新代碼
if #available(iOS 15, *) {
let bar = UITabBarAppearance.init()
bar.backgroundColor = UIColor.white
bar.shadowImage = UIColor.init(0xEEEEEE).image
let selTitleAttr = [
NSAttributedString.Key.font: itemFont,
NSAttributedString.Key.foregroundColor: UIColor.theme
]
bar.stackedLayoutAppearance.selected.titleTextAttributes = selTitleAttr // 設(shè)置選中attributes
self.tabBar.scrollEdgeAppearance = bar
self.tabBar.standardAppearance = bar
}