UINavigationBar not updating on 'back' in

2019-04-10 12:39发布

问题:

I'm working on a UINavigationController driven iPad app (testing in the simulator). There are only two UIViewControllers on the nav controllers stack. For demonstration, lets call them SetupController and ContentController. SetupController pushes a ContentController on the stack with

[self.navigationController pushViewController:contentController animated:YES];

While looking at the content, you can press the back button to go back to the setup controller. If the app is in portrait mode, things work correctly.

However, when the app is in landscape and I hit the back button, things go haywire. The view controller stack is updated properly (e.g. I see the SetupController's view), but the UINavigationBar is not updated properly. The UINavigation bar items associated with the ContentController are still displayed. To see the SetupCotroller's expected UINavigationBar items, I have to press the back button a second time, at which point the UINavigationBar animates to the correct state. Again, this only happens in landscape mode, portrait mode works perfectly.

As a test. In the [SetupController viewDidAppear:] method I have added the following debug output

 if(self.navigationController.navigationBar.topItem != self.navigationItem) {
  NSLog(@"wrong nav item!");
 } else {
  NSLog(@"correct nav item!");
 }

I get the "wrong" message whenever the simulator is in landscape mode, and never when it is in portrait mode. I've tried removing all viewDidAppear: messages from both ViewControllers and any instances where I'm modifying their navigation items or the navigation bar itself.

Any thoughts? I'm assuming I'm doing something wrong here, but this sure feels like a bug to me.

回答1:

I ran into the same problem. It's weird, but you need to make sure all of the view controllers in the stack have the following implemented (even if everything is displaying correctly rotated):

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }