iOS基础iOS接下来要研究的知识点将来跳槽用

iOS 15 适配导航栏和uitabbar还有额外的填充表视图标

2021-10-12  本文已影响0人  白色天空729

iOS 15 之 顶部导航栏设置颜色失效、uitableView 头部视图艰巨高度变大、uitabbar横线消失。
问题界面:


image.png

在Appdelegate的finishLauchwithOptions方法中加入
swift版本

if #available(iOS 15, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.backgroundColor = UIColor(red: 0.0/255.0, green: 125/255.0, blue: 0.0/255.0, alpha: 1.0)
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
            
    /// 额外的填充表视图标题在iOS 15
    UITableView.appearance().sectionHeaderTopPadding = 0
    let color = UIColor.white            
    ///修复uitabbar 顶部视图莫名横线消失
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = color
    UITabBar.appearance().standardAppearance = tabBarAppearance
    UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
// 设置tabbar 为选中文字颜色
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.white
} else {
// 设置tabbar 未选中文字颜色
UITabBar.appearance().unselectedItemTintColor = UIColor.red
// 设置tabbar 选中文字颜色
UITabBar.appearance.tintColor = UIColor.gray
}

OC版本:

if (@available(iOS 15, *)) {
     UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
     [appearance configureWithOpaqueBackground];
     // 背景色
     appearance.backgroundColor = ThemeTool.et_blueBgColor;

     // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
     appearance.shadowColor = [UIColor clearColor];
     // 字体颜色
     appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

     // 带scroll滑动的页面
     UINavigationBar.appearance.scrollEdgeAppearance = appearance;
     // 常规页面
     UINavigationBar.appearance.standardAppearance = appearance;
     
     UITableView.appearance.sectionHeaderTopPadding = 0.f;
     
     UIColor *color = [UIColor whiteColor]; // #F5F5F5 for white smoke.
     UITabBarAppearance *tabBarAppearance = [UITabBarAppearance new];
     tabBarAppearance.backgroundColor = color;
     [[UITabBar appearance] setStandardAppearance:tabBarAppearance];
     [[UITabBar appearance] setScrollEdgeAppearance:tabBarAppearance];
// 设置tabbar 未选中文字颜色
     tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes =
     @{NSForegroundColorAttributeName : UIColor.whiteColor};
     // 设置tabbar 选中文字颜色
     tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes =
     @{NSForegroundColorAttributeName : ThemeTool.et_blueBgColor};
 } else {
     // 设置tabbar 未选中文字颜色
     [[UITabBar appearance] setUnselectedItemTintColor:UIColor.redColor];
     // 设置tabbar 选中文字颜色
     [UITabBar.appearance setTintColor:UIColor.grayColor];
 }

修复后:


image.png

本例子github链接:
https://github.com/Dongxi729/testiOS15NavigatonIssue

参考链接:
https://stackoverflow.com/questions/69111478/ios-15-navigation-bar-transparent

上一篇下一篇

猜你喜欢

热点阅读