How to support only portrait mode on an iPhone app

Go To StackoverFlow.com

4

I have a strange problem in an iPhone app I'm developing. I want my app to support ONLY portrait mode, but for some reason I can't do it (device & simulator).

To support only portrait mode I did as follow:

  • In the TARGET summary section on Xcode, I chose only portrait.
  • All my ViewControllers implements shouldAutorotateToInterfaceOrientation

But as I said it won't work, and the strange result is that the app support ALL the orientations (portrait, upside down, landscape left, landscape right).
Any ideas?

this how I implement shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     // Return YES for supported orientations
     NSLog(@"Checking orientation %d", interfaceOrientation);
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

I notice just now that when I rotate the phone I get this message:

"Two-stage rotation animation is deprecated. This application should use the smoother single-stage animation."

What does it means?

2012-04-05 16:36
by Eyal
return NO from every where - Nilesh 2012-04-05 18:13
Try my answer. Maybe it will help - rohan-patel 2012-04-06 10:45
Try adding NSLog(@"Checking orientation %d", interfaceOrientation); to your shouldAutorotateToInterfaceOrientation methods. Are any of them being called when you rotate the simulator or your device - Dondragmer 2012-04-07 22:48
Thanks for your help, yes the log print "Checking orientation 1". But I noticed something else, please check my updated questio - Eyal 2012-04-08 07:57
@Eyal: the additional console messages probably mean your code implements the old, and deprecated, didAnimateFirstHalfOfRotationToInterfaceOrientation:, and the similar willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: and willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:. You can probably do without these methods - Dondragmer 2012-04-10 13:51
@Eyal: interfaceOrientation of 1 is UIInterfaceOrientationPortrait, which is what you're expecting. If that really happens when you're rotating to landscape, this is weirder than ever: the device returns YES because it thinks it's rotating to portrait. Can you post a complete shouldAutorotateToInterfaceOrientation method - Dondragmer 2012-04-10 13:57
@Dondragmer: can it be that the old implementation of didAnimateFirstHalfOfRotationToInterfaceOrientation causes the problem? (I posted the complete method in my question - Eyal 2012-04-12 10:46
@Eyal: The didAnimate... methods should have no effect. shouldAutorotateToInterfaceOrientation: is all that matters. What gets logged if you turn your device to each of the four orientations? Does it display Checking orientation 1 four times - Dondragmer 2012-04-12 11:03
@Dondragmer: I was sure that I got 'Checking orientation 1' every time I rotate the device before, but now I see that I only get it the first time the viewController become visible, but when I rotate the device(or simulator) I only get the "Two-stage rotation animation is deprecated..." with no call to the 'shouldAutorotateToInterfaceOrientation' metho - Eyal 2012-04-12 11:46
@Eyal: Did you implement shouldAutorotateToInterfaceOrientation: for all your viewControllers? It only gets called on whichever one is active at the time - Dondragmer 2012-04-12 11:53
@Dondragmer: Yes of course, does shouldAutorotateToInterfaceOrientation supposed to be call every time I rotate the device - Eyal 2012-04-12 12:19
@Eyal: Yes, but it will only be called on whichever viewController is in the foreground. How many viewControllers do you have? Do their shouldAutorotateToInterfaceOrientation: methods all contain this logging statement - Dondragmer 2012-04-12 12:24
@Dondragmer: Yes I copy-paste this method for all of them. I have 3 main viewControllers that are loaded when the app launch and used within a UITabBarController - Eyal 2012-04-12 12:33
UITabBarController is itself a ViewController. The default implementation seems to respond to shouldAutorotateToInterfaceOrientation: by querying its subviews, but if you created your own subclass of UITabBarController, it may have its own version. Did you subclass UITabBarController - Dondragmer 2012-04-12 12:43
@Dondragmer: You are a genius! Yes I subclass UITabBarController although I didn't implemented shouldAutorotateToInterfaceOrientation in it, but when I did implemented it with UIInterfaceOrientationPortrait it worked! Thanks for your patience, can I rate your answer somehow - Eyal 2012-04-12 13:34
@Eyal: Thank you. I've posted an answer, should you choose to accept it - Dondragmer 2012-04-12 13:38


4

It is possible to have multiple ViewControllers on the screen. The UITabBarController is itself a UIViewController, and it only passes shouldAutorotateToInterfaceOrientation: requests to the viewControllers within if it chooses. The default implementation does this, but if you subclass it, the code XCode generates (as of iOS 5.1) does not.

2012-04-12 13:37
by Dondragmer


9

On the Target Summary choose portrait only.

2012-04-05 16:37
by Eric
As i wrote, I did that already : - Eyal 2012-04-05 16:40
try deleting the shouldAutorotateToInterfaceOrientation methods and keeping the target set correctly - Eric 2012-04-05 16:43
sorry doesn't wor - Eyal 2012-04-05 16:53


3

Go to info.plist file. Right Click open it as source code. And look for this line. For me in iPad its like this:

  <key>UISupportedInterfaceOrientations~ipad</key>

Delete all other orientation and keep the only one which you need..Like this :

    <array>

    <string> UIInterfaceOrientationPortrait </string>

</array>
2012-04-05 18:09
by rohan-patel
Thanks but I did that alread - Eyal 2012-04-06 10:47
Then your issue is strange - rohan-patel 2012-04-06 10:48


1

check your plist and make sure the key there is set correctly.

2012-04-05 16:38
by Jesse Naugher
I checked that and it is set to portrait.. - Eyal 2012-04-05 16:41
Ads