I have a UIViewController that returns YES in shouldAutorotateToInterfaceOrientation: for UIDeviceOrientationPortrait and NO for everything else. With that view on the top of the stack, I use pushViewController:animated: to push a new UIViewController. The new controller returns YES for anything in shouldAutorotateToInterfaceOrientation:.
The first view refuses to rotate (as expected). Once the second view is pushed, the user can rotate the device and the UI will rotate (also as expected). If the second view is in landscape mode and the user presses the back button (which calls popViewControllerAnimated:), the first view will appear rotated (unexpected!).
If the user rotates the device back to portrait orientation, the view will rotate and then be stuck in portrait mode as before. This works but it’s ugly for the user until they rotate back. So I’m in search of a way to make this view stay in portrait mode.
The only workaround I have found so far is to use -[UIDevice setOrientation:], which throws a warning (orientation is read-only) but works since it is actually defined. This is a huge hack and I’d like a real solution. In search of a real solution I attached GDB to the Photos application (MobileSlideshow.app) and discovered that it too uses -[UIDevice setOrientation:]. Being an internal application though I guess they have different rules.
Is there a correct way to achieve the expected autorotation behavior?
I was about to tell you that there was probably no way, but then I had a thought. It would be difficult to get right, but you might be able to make it work if you used two separate
UINavigationControllers: one that controls the root view and prohibits rotation, and one for the child views that allows it. You would manually handle the transition to and from the root controller and the child controller.You’d have to patch up the child navigation controller to have the correct back button. And, of course, you’d have to handle the back button press yourself. You would probably have to use a dummy
UINavigationBarto do the animation from one navigation controller to the next so that the transition will look right. You would also have to animate the “push” transition between the navigation controllers, as well, which might take a bit of tweaking to get it to look right. You would have to:UINavigationItemand push it on)UINavigationItemfor the incoming view controller and push it on the dummy navigation barUINavigationBarfrom the view, and also the outgoing navigation controller.All of this is a lot of work, but if you’re very clever (and very tenacious), you might be able to get it to work. I’d love to see the result!
That said, you might be better off just using
setOrientation:and taking your chances with the App Store approval process 😉