[iOS] iOS15+設(shè)置導(dǎo)航背景色不起作用問(wèn)題

在自定義設(shè)置導(dǎo)航背景色時(shí),使用下面的方法,在iOS15+系統(tǒng)上不起作用:

UINavigationBar *appearance = [UINavigationBar appearance];
//        [appearance confi];
        [appearance setBarTintColor:[UIColor whiteColor]];
        [appearance setTintColor: [UIColor whiteColor]];
        appearance.translucent = NO;
        NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];
        textAttribute[NSForegroundColorAttributeName] = MPColorBlue;//標(biāo)題顏色
        textAttribute[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];//標(biāo)題大小
        [appearance setTitleTextAttributes:textAttribute];
        
        UIColor *color = MPMainColor;
        UIImage *backGroundImage = [UIImage imageWithColor:color];
        backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
        [appearance setBackgroundImage:backGroundImage forBarMetrics:UIBarMetricsDefault];
//        appearance.backgroundColor = color;
    
        //去除底部黑線
        [appearance setShadowImage:[UIImage new]];

但是,如你的頁(yè)面有scrollView,例如table、webView這些,滑動(dòng)的時(shí)候,導(dǎo)航顏色會(huì)變?yōu)槟阍O(shè)置的顏色,但是一旦滑動(dòng)到頂部,導(dǎo)航又會(huì)不正常,變成黑色;

查了資料,說(shuō)在iOS15+使用下面的方法:

UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    
    [appearance configureWithOpaqueBackground];
    NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];
    textAttribute[NSForegroundColorAttributeName] = MPColorBlue;//標(biāo)題顏色
    textAttribute[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];//標(biāo)題大小
    [appearance setTitleTextAttributes:textAttribute];
    
    //去除底部黑線
    [appearance setShadowImage:[UIImage new]];
    
    UIColor *color = MPMainColor;
     appearance.backgroundColor = color;
    
    self.navigationBar.standardAppearance = appearance;

該方法表現(xiàn)和上面一致;

最終在一個(gè)文章里看到這個(gè)屬性:

self.navigationBar.scrollEdgeAppearance

需要把這個(gè)也設(shè)置了才會(huì)表現(xiàn)正常,最終完整的設(shè)置如下:

UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    
    [appearance configureWithOpaqueBackground];
    NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];
    textAttribute[NSForegroundColorAttributeName] = MPColorBlue;//標(biāo)題顏色
    textAttribute[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];//標(biāo)題大小
    [appearance setTitleTextAttributes:textAttribute];
    
    //去除底部黑線
    [appearance setShadowImage:[UIImage new]];
    
    UIColor *color = MPMainColor;
    appearance.backgroundColor = color;
    
    self.navigationBar.standardAppearance = appearance;
    self.navigationBar.scrollEdgeAppearance = appearance;

我這里設(shè)置的 backgroundColor,你也可以設(shè)置BackgroundImage

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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