In Swift, how to get the device orientation correctly right after it's launched?

后端 未结 5 1832
孤独总比滥情好
孤独总比滥情好 2021-01-12 22:45

I use following code to see if the device is in landscape mode or not:

UIDevice.currentDevice().orientation.isLandscape.boolValue

It works

5条回答
  •  暖寄归人
    2021-01-12 23:15

    I had a problem to detect which orientation was before isFlat so I put this in my view controller

    let orientation = UIDevice.current.orientation
    
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if  orientation.isPortrait {
            return .portrait
        } else if orientation.isFlat{
            if UIScreen.main.bounds.width < UIScreen.main.bounds.height{
                return .portrait
            } else {
                return .landscape
            }
        } else {
            return .landscape
        }
    }
    

提交回复
热议问题