Swift - Allow Rotation on iPad Only

后端 未结 5 856
小鲜肉
小鲜肉 2020-12-13 01:51

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 t

相关标签:
5条回答
  • 2020-12-13 02:28

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:37

    If you want to set this for a specific ViewController (allow all on iPad but only portrait on iPhone) in Swift 4:

    override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
        return UIDevice.current.userInterfaceIdiom == .pad ? UIInterfaceOrientationMask.all : UIInterfaceOrientationMask.portrait
    }
    
    0 讨论(0)
  • 2020-12-13 02:39

    I'm not sure if Chris's answer works by just copying and pasting "Supported Interface orientations (iPad)" key; probably not judging from the XML source of info.plist. for achieving different orientation support. you can open the info.plist XML source and edit it as follows:

    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    
    
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array> 
    

    And there is an easeir approche from xcode UI. Go to your project settings. select your target in the "General" tab and then in "Deployment Info" section you can first select iphone/ipad and mark the device orientations you want to support for each device sepratly and then change the devices to "Universal". it will generate the above xml under the hood.

    the "Universal" option here shows the selections that are common between iPhone and iPad.

    0 讨论(0)
  • 2020-12-13 02:40

    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

    0 讨论(0)
  • 2020-12-13 02:40

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

    The language you use doesn't change the architecture of the application. You do this in Swift the same way you do it in Objective-C, i.e. by implementing:

    optional func application(_ application: UIApplication,
             supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
    

    in your application delegate.

    0 讨论(0)
提交回复
热议问题