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

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

-(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];
}

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

iOS导航栏标题错乱的解决方法_第1张图片
-(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==(
"<: title:'ViewController'>",
"<: title:'ViewController_1'>",
"<: title:'ViewController_2'>"
)
2017-04-23 14:22:15.811 导航栏[2008:53347]
ViewController_3==(
"",
"",
"",
""
)```
少一个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];
    }

}

这样就可以了,图


iOS导航栏标题错乱的解决方法_第2张图片

demo传送门

你可能感兴趣的:(iOS导航栏标题错乱的解决方法)