mkale.com

iOS 6 and Wacky Rotations

By October 7, 2012 2:14PM [link]

While updating my apps to iOS 6, I noticed that they were handling rotations poorly. My buttons weren't getting moved correctly when the device switched from portrait to landscape and vice-versa. Digging into the problem, I found that my UIViewControllers were not getting the

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

message on rotation. I know that iOS 6 deprecates -shouldAutorotateToInterfaceOrientation, but near as I could tell -didRotateFromInterfaceOrientation should still be ok. After scratching my head a bit, I noticed that my newer apps did not have the problem, only my apps that have been around since the (then)iPhoneOS 2.x days. With this clue, I was eventually able to track the problem down to some changes in the template app code that Xcode generates for you. Somewhere along the line, in -application:didFinishLaunchingWithOptions

[self.window addSubview:[self.myRootViewController view]];

changed to:

self.window.rootViewController = self.myRootViewController;

The rootViewController property was introduced in iOS 4. My apps that had been around for a while still had the old template code, which unbeknownst to me was working fine up until iOS 5. Changing my older apps to set the rootViewController property fixed my rotation problems. The documentation does indicate that the latter is now the preferred way of doing things, so this is a good change to make for every app that does not need to run on iOS 3.x (which should be very few apps at this point).

So: if you're running into rotation wackiness on iOS 6, check to see if your -didRotationFromInterfaceOrientation is getting called, and if it's not check your app delegate to make sure you're setting UIWindow's rootViewController property rather than just adding a subview.

History | Blog | mkale.com