Set navigation bar hidden, depending on how the vi

2019-02-16 20:03发布

I have a tab bar with a navigation controller in one of the tabs. Currently the root view of the navigation controller doesnt have the nav bar showing and animates nicely into the subviews by

- (void)viewDidLoad {
   ...
   [self.navigationController setNavigationBarHidden:YES animated:NO];
   ...
}

and

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

But of course changing tabs initiates the viewWillAppear function and so as I go back to the root view the navigation bar slides away, rather than just not being there.

Is there a way that I can hide the nav bar on the root view without animating it except for when appearing from a subview on the navigation stack?

3条回答
手持菜刀,她持情操
2楼-- · 2019-02-16 20:34

Try showing/hiding the bar in the UINavigationController's delegate's navigationController:willShowViewController:animated: method, depending on whether the view controller being shown is your root view controller.

查看更多
Animai°情兽
3楼-- · 2019-02-16 20:39

What if you set a boolean variable in your application delegate and in set that boolean accordingly in subviews as 0 and in other views as 1. And in your viewwillappear, according to your variable's value, you can set the animation.

查看更多
Fickle 薄情
4楼-- · 2019-02-16 20:45

The (BOOL)animated parameter on viewWillAppear:animated. When changing Tabs, it will come as NO, since the animation is immediate. On the other hand, if it's being pushed or popped from the navigation stack with animated:YES, then it will come as YES.

Although this looks like a hack, it's the correct way: you don't need to figure out who was the caller, instead, focus on the fact that if your view controller will appear animated, you have time to do your own animations, if not, screw it, show (or in this case, hide) everything immediately.

查看更多
登录 后发表回答