Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable landscape orientation?

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?

like image 572
user1483652 Avatar asked Aug 02 '12 20:08

user1483652


People also ask

How do I turn off landscape orientation?

Add android:screenOrientation="portrait" to the activity you want to disable landscape mode in.

Why is my phone stuck in landscape mode?

The Basic SolutionsIf the screen rotation is already on try turning it off and then on again. To check this setting, you can swipe down from the top of the display. If it's not there, try going to Settings > Display > Screen rotation.


4 Answers

To disable orientations for a particular View Controller, you should now override supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {     // Return a bitmask of supported orientations. If you need more,     // use bitwise or (see the commented return).     return UIInterfaceOrientationMaskPortrait;     // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; }  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {     // Return the orientation you'd prefer - this is what it launches to. The     // user can still rotate. You don't have to implement this method, in which     // case it launches in the current orientation     return UIInterfaceOrientationPortrait; } 

If you're targeting something older than iOS 6, you want the shouldAutorotateToInterfaceOrientation: method. By changing when it returns yes, you'll determine if it will rotate to said orientation. This will only allow the normal portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {     // Return YES for supported orientations     return (interfaceOrientation == UIInterfaceOrientationPortrait);           // Use this to allow upside down as well     //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } 

Note that shouldAutorotateToInterfaceOrientation: has been deprecated in iOS 6.0.

like image 194
thegrinner Avatar answered Sep 23 '22 00:09

thegrinner


Xcode 5 and above

  • Click your project in the Project Navigator in the left sidebar to open the project settings
  • Go to the General tab.
  • Uncheck the options you don't want in the Deployment Info section, under Device Orientation

screenshot showing where to click

like image 27
skywinder Avatar answered Sep 23 '22 00:09

skywinder


Xcode 4 and below

For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):

enter image description here

It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.

like image 23
Thomas Verbeek Avatar answered Sep 21 '22 00:09

Thomas Verbeek


Most simple solution separate for iPhone and iPad (Universal) - its remove unnecessary orientation in the info.plist file or Project -> Info -> Custom iOS Target Properties.

Just add or remove orientation item from list:

  • Supported interface orientation for iPhone
  • Supported interface orientations (iPad) for iPad

enter image description here

like image 21
Anonimys Avatar answered Sep 24 '22 00:09

Anonimys