在自定義設(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