Since my rootViewController was the only one getting its shouldAutorotate
and supportedInterfaceOrientations
methods called, I decided to register every other view controller to get notified of any UIDeviceOrientationDidChangeNotification
.
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (void)orientationChanged:(NSNotification *)notification
{
NSLog(@"Orientation changed!");
[self layoutForOrientation:[[UIDevice currentDevice] orientation]];
}
Within my layoutForOrientation:
method I handled the uiview's controls orientation.
However, although I did receive UIDeviceOrientationDidChangeNotification
notifications normally, my view orientation would not actually change to match the current orientation, i.e. if the new orientation was say, UIInterfaceOrientationLandscapeRight
, the view orientation would remain in UIInterfaceOrientationPortrait
and its children views would get rotated by 90 degrees. This did certainly not look right. After pulling a lot of hair out, I decided to abandon this route.
Instead, I have set my rootViewController to be a UINavigationController and have it push successive UIViewControllers on top of it. Since I did not want a navigation bar visible in my app I have set the navigation controller's navigationBarHidden
property set to YES. This way the method willAnimateRotationToInterfaceOrientation
is getting called on every UIViewController that is currently at the top of the navigationCotroller's stack of controllers, and its corresponding view and view controls rotate correctly for the desired orientations.
This works because most of my view controllers supported interface orientations match the mask: UIInterfaceOrientationMaskAllButUpsideDown
, the default behaviour for iPhone. Should I needed to support different orientations, I would have to subclass the UINavigationController and override supportedInterfaceOrientations
and shouldAutorotate
to enable the desired orientations support for the navigation stack's top view controller, as per Apple's example project: AlternateViews.