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

后端 未结 5 1865
孤独总比滥情好
孤独总比滥情好 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:11

    I have tested many times about orientation, so I have summed up some experience.

    In all iPhone devices, except iPhone6(s) plus, the only interface orientation is .portrait. If App is launched in landscape mode, there must be a change of orientation. One will receive the UIDeviceOrientationDidChangeNotification. It's an appropriate time to get the orientation.

    Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:

    The launching when in landscape with iPhone6 plus, after launch the orientation never changed:

    Two different screenshot with the same app,

    So before the app does change orientation, the orientation is still like in the home page.

    In viewDidLoad, the orientation has not changed yet, the log will be the wrong direction.

提交回复
热议问题