Swift - Allow Rotation on iPad Only

后端 未结 5 869
小鲜肉
小鲜肉 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)
        }
    }
    

提交回复
热议问题