先看情況:
查資料發現iOS15 中
UITableView
新加了一個屬性:
sectionHeaderTopPadding
,
automaticDimension
,就會導致頂部多出一條空白。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //加這里 if (@available(iOS 15.0, *)) { self.tableView.sectionHeaderTopPadding = 0; } }
1.iOS15更新之后 導航條突然就白了?一招教你變回來~
1.導航欄變白
2.導航欄字體變黑
3.tabbar
OC:
1.導航欄
BaseNavigationController的viewDidLoad方法里添加:
- (void)viewDidLoad { [super viewDidLoad]; if (@available(iOS 13.0, *)) { UINavigationBarAppearance *appearance = [UINavigationBarAppearance new]; [appearance configureWithOpaqueBackground]; // 改變導航欄的顏色 appearance.backgroundColor = self.configuration.barTineColor; // 改變導航欄的標題顏色 appearance.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]}; //導航欄包含狀態欄 陰影線顏色背景色設置 appearance.shadowColor = self.configuration.barTineColor; // 靜止樣式 self.navigationBar.standardAppearance = appearance; // 滾動樣式 self.navigationBar.scrollEdgeAppearance = self.navigationBar.standardAppearance; } }
2.Tabbar
BaseTabBarController 的 viewDidLoad方法里添加:
- (void)viewDidLoad { [super viewDidLoad]; if (@available(iOS 13.0, *)) { UITabBarAppearance *appearance = [UITabBarAppearance new]; [appearance configureWithOpaqueBackground]; appearance.backgroundColor = [UIColor hexColor:0x111423]; self.tabBar.standardAppearance = appearance; /// 這里有點區別。導航欄是iOS13開始。這里是iOS15才有的 if (@available(iOS 15.0, *)) { self.tabBar.scrollEdgeAppearance = self.tabBar.standardAppearance; } } }
Swift:
1.導航欄
if #available(iOS 13.0, *) { let app = UINavigationBarAppearance() app.configureWithOpaqueBackground() // 重置背景和陰影顏色 app.backgroundEffect = nil //這里設置透明或者不透明 app.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18), NSAttributedString.Key.foregroundColor: UIColor.white ] app.backgroundColor = .clear // 設置導航欄背景色 app.shadowColor = nil UINavigationBar.appearance().scrollEdgeAppearance = nil // 帶scroll滑動的頁面 UINavigationBar.appearance().standardAppearance = app // 常規頁面。描述導航欄以標准高度 }
2.Tabbar
// tabBar if #available(iOS 13.0, *) { let itemAppearance = UITabBarItemAppearance() itemAppearance.normal.titleTextAttributes = [.foregroundColor: NorMal_Color ?? .green] itemAppearance.selected.titleTextAttributes = [.foregroundColor: Selected_Color ?? .orange] let appearance = UITabBarAppearance() appearance.stackedLayoutAppearance = itemAppearance appearance.configureWithOpaqueBackground() appearance.backgroundColor = .white tabBarController?.tabBar.standardAppearance = appearance if #available(iOS 15.0, *) { tabBarController?.tabBar.scrollEdgeAppearance = tabBarController?.tabBar.standardAppearance } else { // Fallback on earlier versions } }
參考:https://baijiahao.baidu.com/s?id=1711749740139600655&wfr=spider&for=pc
造成這個原因是什么吶?
答: 兩個因素.
-
scrollEdgeAppearance 屬性
-
iOS15 強制適用於所有導航器
當導航控制器包含導航欄和滾動視圖時,滾動視圖的部分內容將顯示在導航欄下方。如果滾動內容的邊緣到達該欄,UIKit將在此屬性中應用外觀設置。如果此屬性的值為nil,UIKit將使用standardAppearance屬性中的設置,並修改為使用透明背景。如果沒有導航控制器管理您的導航欄,UIKit將忽略此屬性,並使用導航欄的標准外觀。在使用iOS 14或更低版本的應用程序上運行時,此屬性適用於標題較大的導航欄。在iOS 15中,此屬性適用於所有導航欄。
如何解決.
@NSCopying var scrollEdgeAppearance: UINavigationBarAppearance? { get set }
我們只需要按照UIKit 的最新改動進行適配就好.如下上:

如果設置導航欄透明 ,也會無效。
原因:新的導航欄 在加入 large 模式之后 apple 會對普通模式的 nav 的 _UIbarBackground 進行一次 alpha = 1 的設置。
我們直接改變其 subview 的 alpha 就好了。
解決方法:
3. backgroundImage:背景圖片
4. backgroundImageContentMode : 渲染 backgroundImage 時使用的內容模式。 默認為 UIViewContentModeScaleToFill 。
5. shadowColor:底部分割線陰影顏色
6. shadowImage: 陰影圖片
//新建一個導航欄 UINavigationBarAppearance *appearance = [UINavigationBarAppearance new]; //導航欄包含狀態欄重置背景和陰影顏色 [appearance configureWithOpaqueBackground]; //導航欄包含狀態欄背景色設置 appearance.backgroundColor = [UIColor colorWithRed:95.0/255.0 green:177.0/255.0 blue:53.0/255.0 alpha:1.0]; //導航欄包含狀態欄 陰影線顏色背景色設置 appearance.shadowColor = [UIColor colorWithRed:95.0/255.0 green:177.0/255.0 blue:53.0/255.0 alpha:1.0]; // 常規頁面。描述導航欄以標准高度 self.navigationBar.standardAppearance = appearance; //導航欄包含狀態欄帶scroll滑動的頁面 self.navigationBar.scrollEdgeAppearance = self.navigationBar.standardAppearance; //設置導航欄為不透明 self.navigationController.navigationBar.translucent = NO;;//iOS7之后由於navigationBar.translucent默認是YES,原點在(0,0)點,當設置NO的時候,原點坐標在(0,64)點 //設置導航欄bar默認顏色 self.navigationController.navigationBar.barTintColor = [UIColor redColor]; //設置導航欄兩側控件顏色(文字顏色) self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //設置導航欄圖片顏色做背景,沒有圖片iOS13前默認是透明狀態 self.navigationController.navigationBar.shadowImage = [UIImage new]; [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.subviews.firstObject.alpha = 0; ////2. backgroundColor:注意 這個屬性在 backgroundImage 下(在某個界面單獨設置導航欄顏色,直接使//用 backgroundColor 無效,被 backgroundImage 遮住了) //如果設置導航欄透明 ,也會無效。 //原因:新的導航欄 在加入 large 模式之后 apple 會對普通模式的 nav 的 _UIbarBackground 進行一次 alpha //= 1 的設置。 //我們直接改變其 subview 的 alpha 就好了。