Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7: UINavigationBar out of sync with UINavigationController

Tags:

ios

ios7

I have the following code:

NSArray* stack = self.navigationController.viewControllers;
NSArray* newStack = @[stack[0], stack[2]];
[self.navigationController setViewControllers:newStack animated:NO];

stack contains 3 view controllers. The problem is that the navigation bar is not removing UINavigationItems to match, so self.navigationController.navigationBar.items.count still returns 3 after running this code. Going back gets you into a weird state where you have a back button at the top that you can press but it just disappears, not taking you back any further.

Is this a bug in iOS 7 or am I just trying to do something really stupid? What's the best way to fix or work around this?

like image 964
n s Avatar asked Sep 24 '13 20:09

n s


1 Answers

The navigationBar has its own ‘items’ stack which is not updated until viewDidAppear hits.

Which means, if we recreate the navigation controllers’ stack in viewDidLoad using i.e. setViewControllers: when we get to viewDidAppear we will have the current item added to the bars’ ‘items’ stack and therefore the UINavController viewController stack will not be in sync with the UINavBar items stack. This appears to be an iOS 7 bug.

In iOS 6.0 the 2 different stacks do not get out of sync no matter where we set the new viewControllers stack.

So try moving your code in viewDidAppear and see if that fixes the problem. I bet it will, because for me it did.

like image 165
Horatiu Paraschiv Avatar answered Sep 22 '22 00:09

Horatiu Paraschiv