iOS 15导航栏设置

背景

使用Xcode 13.0运行项目到iOS 15的手机上,出现导航栏黑色。但是在低版本Xcode 运行到手机就没有问题。

修改

设置方法需修改,参考barTintColor not working in iOS 15

原来设置导航栏代码不变,新增设置UINavigationBarAppearance实例对象的属性,然后赋值到全局的 navigationBar 或者单个页面的 navigaitonBar 属性中,取决于项目的设置是全局 NavigationBar 还是单个页面设置(可参考iOS StatusBar 设置)。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

- (void)updateNavigationBarColor:(UIColor *)color {
UINavigationBar *bar = self.navigationController.navigationBar;
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *barAppearance = [UINavigationBarAppearance new];
barAppearance.backgroundColor = color; // 设置背景颜色
barAppearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}; // 设置导航栏字体颜色和大小
barAppearance.shadowColor = [UIColor clearColor]; // 设置导航栏底部的分割线不显示

bar.scrollEdgeAppearance = bar.standardAppearance = barAppearance;
[bar setShadowImage:[UIImage new]];
} else {
// Fallback on earlier versions
}
[bar setBackgroundImage:[UIImage wps_createImageWithColor:color] forBarMetrics:UIBarMetricsDefault];
}

参考