Luke Ryan Jernejcic

Stop Forced Portrait Orientation in iOS App

April 13, 2012

I have an iOS app at the office that I have inherited, and one of the bugs that I found when I started was this:

I have the device in a landscape orientation. I have a UINavigationController with a UITableView (we will term as the “parent”); pushed another UITableView (we will call him the “child”); then pop the child off with the nav back button; and when the view returns to the parent the device orientation always changes to portrait. Event if I try rotating the device at this point, it still stays in a portrait orientation.

I have several similar scenarios in the app, but there was only one exhibiting this problem. Well even though I could not find anywhere in the code that forced a portrait orientation, after searching the web for a little while to find the solution I decide to just try and be a little more explicit with things. So I add this code into the parent view.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    return YES;

Guess what. It works! I am still not sure what the cause was before. I did a quick search through the Apple docs to see what the default is for this method (if there is a super), but I did not see anything about defaults.

I am still new to iOS development, but I am learning not to take anything for granted. Before wasting time search for solutions to bugs, make sure that your code is explicit for the situation. Being explicit also helps make things easier to debug as you can trace though those methods.

Written by Luke Ryan Jernejcic who lives and works in Austin Texas building useful things. Follow him on Twitter