Question

I have the following structure (iOS 7 app):

UIWindow -> UITabBarController -> 2 Tabs
Tab1: NavigationController with HomeViewController as root.
Tab2: NavigationController with OtherViewController as root.

If I rotate the iPad while being in Tab1, HomeViewController executes WillRotateToInterfaceOrientation: without problem. But if I'm in Tab2 and rotate the iPad, willRotate: method of HomeViewController in Tab1 doesn't execute, so when I go back to Tab1 the view's layout is in the wrong orientation and messed up.

What's happening? Thanks in advance for your knowledge.

Was it helpful?

Solution

This is the expected behavior on iOS, since HomeViewController was not being shown while the device was rotated.

Checkout the Apple documentation for supporting multiple view controller interface orientations.

Specifically, the section Rotations May Occur When Your View Controller Is Hidden, that reads:

If your view controller’s contents are not onscreen when a rotation occurs, then it does not see the list of rotation messages. For example, consider the following sequence of events:

Your view controller presents another view controller’s contents full screen. The user rotates the device so that the user interface orientation changes. Your app dismisses the presented view controller. In this example, the presenting view controller was not visible when the rotation occurred, so it does not receive any rotation events. Instead, when it reappears, its views are simply resized and positioned using the normal view layout process. If your layout code needs to know the current orientation of the device, it can read the app object’s statusBarOrientation property to determine the current orientation.

So basically you have to prepare your view controller to update itself according to rotations that may have happened while it was not listening to notifications.

The most common way to do this is to place interface-specific code on viewWillAppear:, since this method is called every time your view controller is shown onscreen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top