I’m confused about the implementation of autorotation in ios.
I have a UIViewController, and I tell it to autorotate inside shouldAutoRotateToInterfaceOrientation. However, it doesn’t work.
So I read something about how I need to look at the navigation stack AND/OR whether a UITabBarController was used, because this has been known to cause all kinds of confusion (raises hand).
In fact, I have a UITabBar and a UINavigationController. The UIView that I want to rotate is being pushed onto the stack three or four ‘levels’ deep.
Why isn’t autorotation solely determined by whatever the shouldAutoRotateToInterfaceOrientation returns inside the current UIViewController?
From apple’s technical doc located at: https://developer.apple.com/library/ios/#qa/qa2010/qa1688.html about why a UIViewController may not be rotating when you expect it to:
All child view controllers in your UITabBarController or UINavigationController do not agree on a common orientation set.
To make sure that all your child view controllers rotate correctly,
you must implement shouldAutorotateToInterfaceOrientation for each
view controller representing each tab or navigation level. Each must
agree on the same orientation for that rotate to occur. That is, they
all should return YES for the same orientation positions.
Can someone summarize what facts we need to be aware of when working with UITabBars and UINavigationControllers? Where do people commonly mess up or what are points of muddiness?
If you have a UITabBar, UITabBarController, or UINavigationController, all of its child views need to have the same autorotation behavior. But surely you can have some child views rotate and others not rotate, right?
I suspect that common reasons for failing have to do with not understanding the responder chain well enough as it applies to autorotation. If so, can someone clear this up for me? Then I might be able to figure out ‘selective autorotation’.
To add to this, other posts indicate that you must subclass UITabBarController and override shouldAutorotateToInterfaceOrientation.
I think the reason you want them all to respond the same way is because it gets awkward for the user. If you are in landscape mode on your special view that can rotate, it will be a strange experience when you pop that viewcontroller to the parent on the navigationcontroller or tap a tab that does not have landscape mode.
I think that is the reasoning there.
However, you can catch the device orientation notifications and handle them yourself if you want and push in a new view if the device rotates on the particular view you want to rotate.
Use this:
then create a function
rotationDetectedthat handles what happens when we rotate…like this: