Reputation: 799
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?
Upvotes: 41
Views: 69189
Reputation: 1737
In Xcode 13.3.1, simply unchecking undesired orientations does not prevent an app from supporting all rotations. It is necessary to enter the Build Settings
tab and manually remove any orientations from the following fields that you do not wish to support:
In my case, my app will now only support portrait orientation.
Upvotes: 10
Reputation: 5853
If you created a new Xcode 13.3 project and unchecked unnecessary orientation checkmarks in the Project > General > Deployment and it didn't help. Check the Target > Build Settings - there are 2 rows which override global settings.
Upvotes: 5
Reputation: 151
I've had the same problem on Xcode 13.0 even though I set the device orientation only Portrait.
Adding these 2 lines to Info.plist solved my problem.
Upvotes: 0
Reputation: 2197
Xcode 8, Xcode 9, Xcode 10 and above
Also, make changes in Info.plist file
Upvotes: 0
Reputation: 12251
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.
Upvotes: 56
Reputation: 704
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:
Upvotes: 13
Reputation: 17942
If you want to disable landscape orientation for both iPhone and iPad.
Go to Targets and Go to the General tab. See the below screen and deselect landscape left and landscape right.
Here in this case only iPhone landscape mode will be disabled not for iPad. For iPad all modes are anabled. If you want select device option from Universal to iPad. It will looks like this. See below screen.
Now you need to deselect all modes except Portrait for iPad. See below screenshot.
Now you successfully disabled all modes except Portrait for all devices.
Upvotes: 6
Reputation: 1828
Swift 3 If you have a navigationController, subclass it like this (for portrait only):
class CustomNavigationViewController: UINavigationController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return UIInterfaceOrientation.portrait
}
}
Upvotes: 2
Reputation: 21436
Upvotes: 48
Reputation: 2431
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):
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.
Upvotes: 28
Reputation: 3477
Removing the method shouldAutorotateToInterfaceOrientation
from your class entirely also works. If you don't plan on rotating then it makes no sense to have the method in your class, the less code the better, keeps things clean.
Upvotes: 0