ios 两个状态栏 背景图片不一样 push过去正常显示
返回(pop)回来,当前的背景图变成了push过去的那个导航栏的背景图片.
用图片演示:
1.这是控制器A:
2.设置push过去的控制器B:
3.点击返回后的控制器A:
为什么控制器A的导航栏的背景图片变成了控制器B的?如何修改?
代码奉上:自定义控制器的所有代码:
#import "LMSNavigationController.h"
@interface LMSNavigationController ()
@end
@implementation LMSNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}
/**
* 在这个方法中拦截所有push进来的控制器
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count > 0) {
// 如果push进来的不是第一个控制器
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setImage:[UIImage imageNamed:@"nav-back.png"] forState:UIControlStateNormal];
[leftBtn setImage:[UIImage imageNamed:@"nav-back-hi.png"] forState:UIControlStateHighlighted];
leftBtn.frame = CGRectMake(0, 0, 30, 30);
[leftBtn addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
// 隐藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
}
//把导航栏底部的横线去掉
[self.navigationBar setShadowImage:[UIImage new]];
// 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
// 意思是,我们任然可以重新在push控制器的viewDidLoad方法中设置导航栏的leftBarButtonItem,如果设置了就会覆盖在push方法中设置的“返回”按钮,因为 [super push....]会加载push的控制器执行viewDidLoad方法。
[super pushViewController:viewController animated:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
控制器A关键代码:
- (void)viewDidLoad {
[super viewDidLoad];
//导航栏透明背景(nav-clearcolor-image是一张透明的图片)
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-clearcolor-image"] forBarMetrics:UIBarMetricsDefault];
//把导航栏底部的横线去掉
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//不显示文字
self.navigationItem.title = nil;
}
控制器B关键代码:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.text = @"push过来的控制器";
titleLabel.textColor = TitleTextColor;
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
nav 下的所有 VC 是通用同一个 navBar 的
你在进入 B 的时候修改了 navBar
但是在退出 B 的时候没有改回来
你有2个导航栏控制器 (pop)回去的导航栏是跟当前的导航栏一样的,应该搞个代理就重新赋值就可以了。(不过比较麻烦)
你肯定在B中修改了导航条的色或者图了吧
在A的viewWillAppear方法里面再设置一遍vc的背景图片试试