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

在iOS 13 UINavigationBar新增了scrollEdgeAppearance屬性,但在iOS 14及更早的版本中此屬性只應(yīng)用在大標(biāo)題導(dǎo)航欄上。在iOS 15中此屬性適用于所有導(dǎo)航欄。
對(duì)于scrollEdgeAppearance屬性的說明:

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.

scrollEdgeAppearanceUINavigationBarAppearance類型,有下面幾個(gè)屬性:

backgroundEffect:

基于backgroundColorbackgroundImage的磨砂效果

backgroundColor:

背景色,在backgroundImage之下

backgroundImage:

背景圖片

backgroundImageContentMode:

渲染backgroundImage時(shí)使用的內(nèi)容模式。 默認(rèn)為UIViewContentModeScaleToFill。

shadowColor:

陰影顏色(底部分割線),當(dāng)shadowImagenil時(shí),直接使用此顏色為陰影色。如果此屬性為nilclearColor(需要顯式設(shè)置),則不顯示陰影。

如果shadowImage包含 template 圖像,則使用該圖像作為陰影并使用此屬性中的值對(duì)其進(jìn)行著色。如果此屬性為nilclearColor(需要顯式設(shè)置),則不顯示陰影。
但是,如果shadowImage不包含 template 圖像,則此屬性無效。

shadowImage:

陰影圖片。
template圖像: [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]

示例代碼:

  • 不透明導(dǎo)純色航欄:
    //navigation標(biāo)題文字顏色
    NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor blackColor],
                          NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]};
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
        barApp.backgroundColor = [UIColor whiteColor];
        barApp.shadowColor = [UIColor whiteColor];
        barApp.titleTextAttributes = dic;
        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
        self.navigationController.navigationBar.standardAppearance = barApp;
    }else{
        //背景色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.navigationController.navigationBar.titleTextAttributes = dic;
        [self.navigationBar setShadowImage:[UIImage new]];
        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
    //不透明
    self.navigationController.navigationBar.translucent = NO;
    //navigation控件顏色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
  • 透明導(dǎo)航欄:
    因?yàn)?code>scrollEdgeAppearance = nil,當(dāng)前如果有ScrollView,當(dāng)ScrollView向上滾動(dòng)時(shí)scrollEdgeAppearance會(huì)默認(rèn)使用standardAppearance。因此backgroundEffectshadowColor也要顯式設(shè)置為nil,防止backgroundEffect、shadowColor出現(xiàn)變成有顏色的。 (更新Xcode13.4.1后和之前似乎不一樣)
    //navigation標(biāo)題文字顏色
    NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor],
                          NSFontAttributeName : [UIFont systemFontOfSize:18]};
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
        barApp.backgroundColor = [UIColor clearColor];
        barApp.titleTextAttributes = dic;   
        barApp.backgroundEffect = nil;
        barApp.shadowColor = nil;
        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
        self.navigationController.navigationBar.standardAppearance = barApp;
    }else{
        self.navigationController.navigationBar.titleTextAttributes = dic;
        [self.navigationBar setShadowImage:[UIImage new]];
        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
    //透明
    self.navigationController.navigationBar.translucent = YES;
    //navigation控件顏色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  • 透明導(dǎo)航欄時(shí)動(dòng)態(tài)修改顏色:
-(Void)setNavigationBarBackgroundColor:(UIColor *)color {
        if (@available(iOS 15.0, *)) {
            self.navigationController.navigationBar.standardAppearance.backgroundColor = color
            self.navigationController.navigationBar.scrollEdgeAppearance.backgroundColor = color
        }else{
            self.navigationController.navigationBar.barTintColor = color
        }
    }

2024年04月12日

push 時(shí)設(shè)置 backgroundColor = .clear沒有過渡動(dòng)畫,可以改變顏色透明度alpha來實(shí)現(xiàn)。比如可以寫成backgroundColor = UIColor(white: 1, alpha: 0)

最后編輯于
?著作權(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)容