Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Allow Rotation on iPad Only

Tags:

ios

swift

ipad

How could I allow my universal app written in Swift on iOS 8.3 SDK to support only portrait mode on iPhone, but both portrait and landscape mode on iPad?

I know in the past this has been done in AppDelegate. How could I do this in Swift?

like image 823
felix_xiao Avatar asked May 29 '15 18:05

felix_xiao


People also ask

How do I force an app to rotate on iOS?

Make sure that Rotation Lock is off: Swipe down from the top-right corner of your screen to open Control Center. Then tap the Rotation Lock button to make sure it's off.

Why do some apps only work in portrait on IPAD?

To reiterate, this issue of the screen being oriented in Portrait mode is controlled by the App. It is the App Developer that codes the App to display in either Portrait or Landscape orientation. Apple has no control over how third-party Apps choose to display their content.

Which setting in an iOS device do you need to enable if you want to test the application for various orientation modes?

From ios 10.0 we need set { self. orientations = newValue } for setting up the orientation, Make sure landscape property is enabled in your project.


2 Answers

You can do it programmatically, or better yet, you can simply edit your project's Info.plist (Which should be more practical, since it's a global device configuration)

Just add "Supported Interface orientations (iPad)" key

enter image description here

like image 177
chrisamanse Avatar answered Sep 25 '22 15:09

chrisamanse


You could do it programmatically

override func shouldAutoRotate() -> Bool {     if UIDevice.currentDevice().userInterfaceIdiom == .Pad {         return true     }     else {         return false     } } 

and then

override func supportedInterfaceOrientations() -> Int {     return UIInterfaceOrientation.Portrait.rawValue } 

or any other rotation orientation that you wish to have by default.

That should detect if the device you're using is an iPad and allow rotation on that device only.

EDIT: Since you only want portrait on iPhone,

 override func supportedInterfaceOrientations() -> Int {     if UIDevice.currentDevice().userInterfaceIdiom == .Phone {         return UIInterfaceOrientation.Portrait.rawValue     }     else {         return Int(UIInterfaceOrientationMask.All.rawValue)     } } 
like image 36
Kilenaitor Avatar answered Sep 25 '22 15:09

Kilenaitor