iOS15系统需要对导航栏进行适配,增加两种appearance,分别是scrollEdgeAppearance
(不滚动时)和standardAppearance
(滚动时)。iOS15系统之前设置导航栏样式后滚动和不滚动是没区别的,现在要实现滚动和不滚动一样的样式必须设置这两种appearance。
下面在控制器里生成一个appearance并赋值给导航栏:(如果需要设置不同的样式可以生成两个appearance对象分别赋值给导航栏)
if (@available(iOS 15.0,*)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
self.navigationController.navigationBar.standardAppearance = self.navigationController.navigationBar.scrollEdgeAppearance;
}
在需要修改样式时可以通过导航栏再获取到这两个appearance,通过appearance修改样式。
注意:
虽然上面生成appearance并且赋值给导航栏时用的是同一个,但是将appearance赋值给导航栏后,导航栏会复制一份,所以导航栏的两种appearance并不是同一个对象。在修改样式时如需修改两种状态下的导航栏样式,必须同时获取到两种appearance再分别进行修改,只获取一个修改是不行的!!!
下面是在某处修改导航栏样式:
if (@available(iOS 15.0,*)) {
UINavigationBarAppearance *appearance = self.navigationController.navigationBar.scrollEdgeAppearance;
// 设置为不透明
appearance.backgroundEffect = nil;
appearance.shadowColor = [UIColor clearColor];
appearance.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor]];
UINavigationBarAppearance *appearance2 = self.navigationController.navigationBar.standardAppearance;
// 设置为不透明
appearance2.backgroundEffect = nil;
appearance2.shadowColor = [UIColor clearColor];
appearance2.backgroundImage = [UIImage imageNamed:@"test"];
} else {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"test"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]]; //去掉导航下面的线
}
网友评论