Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting device orientation in Swift iOS

I am working on a swift app for iPhone. There is a modal view in my application that I want only to be in portrait view.

My question is, how do I programmatically force the phone to not allow rotation? In other words, I am looking for code that will not allow a modal view to be displayed in landscape mode (turning on portrait rotation lock).

This is just for 1 modal view, so I can't turn off rotation for the entire app, otherwise I would just disable rotation altogether.

I found code in my research here But it is in objective C, in case that helps. Thanks!

like image 435
rocket101 Avatar asked Sep 03 '14 19:09

rocket101


People also ask

How do I change the orientation of my device in Xcode?

Historically, if you wanted to restrict your iOS app to specific device orientations, you would check or uncheck the various “Device Orientation” options in your project settings. You can find these by selecting your Xcode Project > App Target > “General” tab. Xcode project "Device Orientation" options.

How do I change orientation in iOS?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.


2 Answers

Hi for LandscapeLeft and LandscapeRight (Update Swift 2.0)

enter image description here And you have this in info

enter image description here

And UIController

override func shouldAutorotate() -> Bool {     return true }  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight] } 

For PortraitUpsideDown and Portrait use that enter image description here

override func shouldAutorotate() -> Bool {     if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||         UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||         UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {             return false     }     else {         return true     } }  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown] } 

Message from France, Merry Christmas !

Edit :

Other solution :

extension UINavigationController {     public override func shouldAutorotate() -> Bool {         if visibleViewController is MyViewController {             return true   // rotation         } else {             return false  // no rotation         }     }      public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {         return (visibleViewController?.supportedInterfaceOrientations())!     } } 
like image 165
YannSteph Avatar answered Oct 01 '22 01:10

YannSteph


You can paste these methods in the ViewController of each view that needs to be portrait:

override func shouldAutorotate() -> Bool {     return false }  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return UIInterfaceOrientationMask.Portrait } 
like image 34
DrOverbuild Avatar answered Oct 01 '22 00:10

DrOverbuild