Getting device orientation in Swift

后端 未结 13 1461
滥情空心
滥情空心 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:43

    Swift 3+

    Basically:

    NotificationCenter.default.addObserver(self, selector: #selector(self.didOrientationChange(_:)), name: .UIDeviceOrientationDidChange, object: nil)
    
    @objc func didOrientationChange(_ notification: Notification) {
        //const_pickerBottom.constant = 394
        print("other")
        switch UIDevice.current.orientation {
            case .landscapeLeft, .landscapeRight:
                print("landscape")
            case .portrait, .portraitUpsideDown:
                print("portrait")
            default:
                print("other")
        }
    }
    

    :)

提交回复
热议问题