iOS15 導(dǎo)航欄屬性設(shè)置及適配

標(biāo)題文字

  • viewController:self.title
  • 返回值是 view 的方法去設(shè)置 titleView

標(biāo)題顏色

// iOS 15之前
bar.titleTextAttributes = @{NSForegroundColorAttributeName: itemColor};
// iOS 15之后
if (@available(iOS 13.0, *)) { // 因?yàn)?api 是從13出的
    UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
    appearance.titleTextAttributes = @{NSForegroundColorAttributeName: itemColor};
    bar.standardAppearance = appearance;
    bar.scrollEdgeAppearance = appearance;
}

導(dǎo)航欄顏色

// iOS 15 之前
// 如果設(shè)置了背景圖片為空?qǐng)D片,必須要禁用穿透效果,才能看到 barTintColor -- self.navigationBar.translucent = NO
navigationBar.barTintColor
或 navigationBar.subviews[0]

// iOS 15 之后
UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
appearance.backgroundColor = UIColor.whiteColor;
...
naviBar.standardAppearance = appearance;
naviBar.scrollEdgeAppearance = appearance;

導(dǎo)航欄控件顏色

naviBar.tintColor

導(dǎo)航欄隱藏

naviBar.hidden = true

導(dǎo)航欄透明 -- 隱藏分隔線

// 法一 navigationContentView 透明
// 參考:《UINavgationBar全透明一行代碼實(shí)現(xiàn)/ 任意顏色》
// 但在 iOS 15 適配了導(dǎo)航欄后,設(shè)置alpha的方法會(huì)使適配無效
navigationBar.subviews[0].alpha = 0;

// 法二 背景色置空 + 陰影圖片置空
// 在 iOS 15 適配了導(dǎo)航欄時(shí)需要顯式標(biāo)明 shadowColor(在 shadowImage 為 nil 時(shí),shadowColor 生效),故雖已經(jīng)隱藏了分割線,但導(dǎo)航欄下方仍會(huì)出現(xiàn)類似分割線的陰影
// 解決方案:需手動(dòng)標(biāo)明 shadowColor 為 clearColor 或 nil
[nav.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[nav.navigationBar setShadowImage:[UIImage new]];
// iOS15 后加入這段代碼 
if (@available(iOS 13.0, *)) { // 因?yàn)?api 是從13出的
    UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
    appearance.shadowColor = UIColor.clearColor;
    bar.standardAppearance = appearance;
    bar.scrollEdgeAppearance = appearance;
}

iOS15 導(dǎo)航欄及tabBar適配

《iOS 15 透明導(dǎo)航欄設(shè)置》

解決 iOS 15 中頁面跳轉(zhuǎn)導(dǎo)航欄暫存及無底色問題
分析:類似 VC,必須要設(shè)置 backgroundColor

- (void)setupAppearance {
    Class baseClass = UINavigationController.class; // 可定制某一個(gè)類型
    UIColor *bgColor = UIColor.whiteColor;
    UIColor *separatorColor = UIColor.redColor;
    NSDictionary *defaultTitleTextAttr = @{NSForegroundColorAttributeName: UIColor.blackColor};

    [UITabBar appearance].barTintColor = bgColor;
    UINavigationBar *naviBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[baseClass]];
    naviBar.barTintColor = bgColor; // 設(shè)置導(dǎo)航欄顏色(ios15 之前生效)
    naviBar.shadowImage = [UIImage imageWithColor: separatorColor];
    [naviBar setBackgroundImage:[UIImage imageWithColor:bgColor] forBarMetrics:UIBarMetricsDefault];
    naviBar.titleTextAttributes = defaultTitleTextAttr;
        
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
        appearance.backgroundColor = bgColor; //設(shè)置導(dǎo)航欄背景色
        appearance.shadowColor = separatorColor; // 設(shè)置分割線默認(rèn)顏色
        appearance.titleTextAttributes = defaultTitleTextAttr; //設(shè)置導(dǎo)航條標(biāo)題顏色
        [UINavigationBar appearance].standardAppearance = appearance;
        [UINavigationBar appearance].scrollEdgeAppearance = appearance;
    }
}

baseVC 提供設(shè)置導(dǎo)航欄接口

目的:提供快捷方法,并在頁面跳轉(zhuǎn)時(shí)自動(dòng)復(fù)原為默認(rèn)效果

// baseVC.h
/// 單頁面設(shè)置隱藏導(dǎo)航欄分割線
@property (nonatomic, assign) BOOL hideNaviBarSeparatorLine;
/// 單頁面設(shè)置導(dǎo)航欄透明度
@property (nonatomic, assign) BOOL isNaviBarTranslucent;
/// 單頁面設(shè)置導(dǎo)航欄標(biāo)題字體
@property (nonatomic, copy) NSDictionary<NSAttributedStringKey, id> *naviBarTitleTextAttributes;

// baseVC.m
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.navigationController &&
        (self.hideNaviBarSeparatorLine || self.isNaviBarTranslucent || self.naviBarTitleTextAttributes.count > 0)) {
        [self base_updateNaviBarStyle];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.navigationController &&
        (self.hideNaviBarSeparatorLine || self.isNaviBarTranslucent || self.naviBarTitleTextAttributes.count > 0)) {
        [self base_resetNaviBarStyle];
    }
}

- (void)base_updateNaviBarStyle {
    UIColor *separatorColor = UIColor.redColor;
    NSDictionary *defaultTextAttributes = @{NSForegroundColorAttributeName: UIColor.blackColor};
    
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:UIImage.new forBarMetrics:UIBarMetricsDefault];
    UIImage *shadowImage = UIImage.new;
    if (!self.hideNaviBarSeparatorLine) {
        shadowImage = [UIImage imageWithColor:separatorColor];
    }
    bar.shadowImage = shadowImage;
    bar.translucent = self.isNaviBarTranslucent;
    NSDictionary *textAttributes = self.naviBarTitleTextAttributes ?: defaultTextAttributes;
    bar.titleTextAttributes = textAttributes;
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
        if  (self.isNaviBarTranslucent) {
            [appearance configureWithTransparentBackground];
        } else {
            [appearance configureWithOpaqueBackground];
        }
        //設(shè)置導(dǎo)航欄背景色
        appearance.backgroundColor = self.isNaviBarTranslucent ? UIColor.clearColor : UIColor.whiteColor;
        appearance.shadowColor = self.hideNaviBarSeparatorLine ? UIColor.clearColor : separatorColor;
        //設(shè)置導(dǎo)航條標(biāo)題顏色
        appearance.titleTextAttributes = textAttributes;
        bar.standardAppearance = appearance;
        bar.scrollEdgeAppearance = appearance;
    }
}


- (void)base_resetNaviBarStyle {
    UIColor *separatorColor = UIColor.redColor;
    NSDictionary *defaultTextAttributes = @{NSForegroundColorAttributeName: UIColor.blackColor};
    
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:UIImage.new forBarMetrics:UIBarMetricsDefault];
    UIImage *shadowImage = [UIImage imageWithColor:separatorColor];
    bar.shadowImage = shadowImage;
    bar.translucent = NO;
    bar.titleTextAttributes = defaultTextAttributes;
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = UINavigationBarAppearance.new;
        [appearance configureWithOpaqueBackground];
        //設(shè)置導(dǎo)航欄背景色
        appearance.backgroundColor = UIColor.whiteColor;
        appearance.shadowColor = separatorColor;
        //設(shè)置導(dǎo)航條標(biāo)題顏色
        appearance.titleTextAttributes = defaultTextAttributes;
        bar.standardAppearance = appearance;
        bar.scrollEdgeAppearance = appearance;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容