Getting device orientation in Swift

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

    statusBarOrientation is deprecated, so no longer available to use like in above answers

    In this code can get orientation without worrying about depreciation. Swift 5 ioS 13.2 Tested 100%

    struct Orientation {
        // indicate current device is in the LandScape orientation
        static var isLandscape: Bool {
            get {
                return UIDevice.current.orientation.isValidInterfaceOrientation
                    ? UIDevice.current.orientation.isLandscape
                    : (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isLandscape)!
            }
        }
        // indicate current device is in the Portrait orientation
        static var isPortrait: Bool {
            get {
                return UIDevice.current.orientation.isValidInterfaceOrientation
                    ? UIDevice.current.orientation.isPortrait
                    : (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait)!
            }
        }
    }
    

提交回复
热议问题