iOS导航栏标题错乱的解决方法

2017-04-23  本文已影响0人  guoshengboy

一般隐藏和显示导航栏,只要在基类如下写就好:

-(BOOL)needHiddenNavigationBar:(UIViewController *)vc{
    
    BOOL isHidden = NO;
    if ([NSStringFromClass([vc class]) isEqualToString:@"ViewController"]||
        [NSStringFromClass([vc class]) isEqualToString:@"ViewController_1"]) {
        
        isHidden = YES;
    }
    return isHidden;
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:[self needHiddenNavigationBar:self] animated:animated];
}

但当在一种特殊情况下,就是在第一第二个控制器隐藏导航栏时,当在第二控制器右滑一点点在回来时在第四个控制器会还是会出现导航栏混乱的情况。

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    NSLog(@"\n%@==%@",NSStringFromClass([self class]),self.navigationController.navigationBar.items);
    NSLog(@"\n%@==%@",NSStringFromClass([self class]),self.navigationController.viewControllers);

}```
结果为:

2017-04-23 14:22:15.810 导航栏[2008:53347]
ViewController_3==(
"<<UINavigationItem: 0x6000001cbb80>: title:'ViewController'>",
"<<UINavigationItem: 0x6080001cbe50>: title:'ViewController_1'>",
"<<UINavigationItem: 0x6080001cc030>: title:'ViewController_2'>"
)
2017-04-23 14:22:15.811 导航栏[2008:53347]
ViewController_3==(
"<ViewController: 0x7fc822103a80>",
"<ViewController_1: 0x7fc8206056c0>",
"<ViewController_2: 0x7fc820510040>",
"<ViewController_3: 0x7fc820615db0>"
)```
少一个item 我偶然看到支付宝也出现此现象 他的做法是在添加一个item。

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    NSLog(@"\n%@==%@",NSStringFromClass([self class]),self.navigationController.navigationBar.items);
    NSLog(@"\n%@==%@",NSStringFromClass([self class]),self.navigationController.viewControllers);
    if (self.navigationController.navigationBar.topItem != self.navigationItem && ![self needHiddenNavigationBar:self]) {
        
        [self.navigationController setNavigationBarHidden:YES animated:animated];
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }

}

这样就可以了,图


demo传送门

上一篇下一篇

猜你喜欢

热点阅读