3

In xcode 4 I am unable to lock the screen orientation to only portrait even though I have portrait selected only. How to I do this programmatically?

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

this code works in a new blank project

Omar
  • 2,155
  • 4
  • 24
  • 38

2 Answers2

10

The accepted answer did not work for me either. This did:

In the properties file for your app (YOURAPPNAME-Info.plist), located in the "supporting files" group, there is an array called "Supported interface orientations". Remove both landscape values from the array and your app will be locked in portrait orientation.

James Harpe
  • 4,315
  • 8
  • 47
  • 74
6

For each view controller (or just the parent), implement -shouldAutorotateToInterfaceOrientation: Set the default orientation to portrait and then do:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}

Alternately, you can do:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return interfaceOrientation == UIInterfaceOrientationPortrait;
}

which will allow a rotation to portrait but not away from it.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • 1
    It seems that its stuck to the settings that I started the project with. I made a new blank app and it works but not in my existing app!!! It's really annoying! – Omar Aug 05 '11 at 22:24