Getting device orientation in Swift

后端 未结 13 1459
滥情空心
滥情空心 2020-11-28 23:23

I was wondering how I can get the current device orientation in Swift? I know there are examples for Objective-C, however I haven\'t been able to get it working in Swift.

13条回答
  •  被撕碎了的回忆
    2020-11-28 23:47

    Apple recently got rid of the idea of Landscape vs. Portrait and prefers we use screen size. However, this works:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.currentDevice().orientation.isLandscape.boolValue {
            print("landscape")
        } else {
            print("portrait")
        }
    }
    

提交回复
热议问题