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?
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:
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.
If you want the application to be running only in Portrait mode,
Below your viewDidLoad
method copy the below line of code
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
This will lock the landscape mode for your application. Similarly by replacing .portrait to .landscape will run your application in landscape mode.
If not specified your application will run on both modes.
Alternatively, you can lock the orientation from Xcode Builder as shown below.