IOS15之UINavigationBar背景颜色不全
环境
xcode 13
ios 15.0
系统升级到ios 15之后,背景颜色,或者背景图片必须滚动的时候,才会显示静止状态的时候的样式。苹果在升级到ios13之后,进行了重大调整。来看看之前的写法
self.navigationController.navigationBar.translucent = NO;
//设置导航栏背景颜色
self.navigationController.navigationBar.barTintColor = k_main_nav_bar_color;
//设置导航栏字体大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:k_title_color,NSFontAttributeName:TEXT_BIG_S_FONT18}];
/*导航栏字体颜色*///左右键颜色图标默认颜色
[self.navigationController.navigationBar setTintColor:k_title_color];
k开头的全部使用自己的宏定义。
新的写法:
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc] init];
//添加背景色
appperance.backgroundColor = k_main_tab_bar_color;
appperance.shadowImage = [[UIImage alloc]init];
appperance.shadowColor = nil;
//设置字体颜色大小
[appperance setTitleTextAttributes:@{NSForegroundColorAttributeName:k_title_color,NSFontAttributeName:TEXT_BIG_S_FONT18}];
self.navigationController.navigationBar.standardAppearance = appperance;
self.navigationController.navigationBar.scrollEdgeAppearance = appperance;
self.navigationController.navigationBar.compactAppearance = appperance;
self.navigationController.navigationBar.compactScrollEdgeAppearance = appperance;
}
compact坚实的意思。坚实的外表
standardAppearance标准的外表
scrollEdgeAppearance 滚动边缘的外表
compactScrollEdgeAppearance 滚动坚实的外表
苹果对appearance 添加了4个属性,一般写standardAppearance和scrollEdgeAppearance即可。
当然以上是在viewController父类中书写。如果全局是UINavigationController父类下,如何书写呢。
- (void)viewDidLoad {
[super viewDidLoad];
if(@available(iOS 15.0,*)){
UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
[appearance setBackgroundImage:[UIImage imageNamed:@"NavBar64"]];
[appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:24 weight:UIFontWeightBold]}];
self.navigationBar.standardAppearance = appearance;
self.navigationBar.scrollEdgeAppearance = appearance;
}else{
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:24 weight:UIFontWeightBold]}];
[self.navigationBar setTintColor:UIColor.whiteColor];
}
}
做一个系统适配