iOS15 如果配置了全局的导航器背景,后面针对某一个导航器修改背景没有效果
SpaceBlueFish opened this issue · comments
empty commented
假如全局设置的背景为红色
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *app = [UINavigationBarAppearance new];
[app configureWithDefaultBackground];
app.backgroundColor = UIColor.redColor;
[UINavigationBar appearance].scrollEdgeAppearance = app;
[UINavigationBar appearance].standardAppearance = app;
}
某一个vc的viewDidLoad方法中,需要修改成白色,没有效果。
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setClipsToBounds:NO];
后来我找到导航器中的背景图片对象,设置bgImgView.image的值,在viewDidAppear中执行可以修改,但滑动返回的时候又被系统重置了,感觉系统在出现和消失的时候都会重置导航器的全局默认属性值。
for (UIView * view in self.navigationController.navigationBar.subviews) {
for (UIView * resultView in view.subviews) {
if ([resultView isKindOfClass:[UIImageView class]]) {
UIImageView * bgImgView = (UIImageView*)resultView;
bgImgView.image = bgImg;
return;
}
}
}
-----------------原来每一个vc也有对应的Appearance,这样设置就行了
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *app = [UINavigationBarAppearance new];
[app configureWithDefaultBackground];
app.backgroundImage = bgImg;
self.navigationItem.scrollEdgeAppearance = app;
self.navigationItem.standardAppearance = app;
return;
}
MyiOS commented
这样有点麻烦啊
Ricky Tan commented
可能是个问题
Lee0820 commented
@rickytan transferNavigationBarAttributes 我觉得还是这个好用,因为大部分的需求还是,整体有默认的导航栏样式,少数的是不同的样式,虽然[RTRootNavigationController]旨在 每一个 VC 有自己单独的导航,但是每个VC都写一遍,还是有点麻烦 0.0,能支持配置默认样式,更好
Ricky Tan commented
用 UIAppearance 统一设置就好