在Swift中,如何在启动后立即正确获取设备方向? [英] In Swift, how to get the device orientation correctly right after it's launched?
问题描述
我使用以下代码查看设备是否处于横向模式:
I use following code to see if the device is in landscape mode or not:
UIDevice.currentDevice().orientation.isLandscape.boolValue
如果我在启动应用程序之前将设备置于横向模式,那么它会起作用,而在viewDidLoad
之后,我调用此代码行,它将始终返回false
.
It works BUT if I put my device in landscape mode before the app is launched, and after viewDidLoad
, I call this line of code, it always returns false
.
如果我改用这个:
interfaceOrientation.isLandscape
它返回true
,这是正确的,但是编译器显示的是interfaceOrientation was deprecated in iOS 8.0
的警告.
it returns true
, which is correct, but the compiler is showing a warning that interfaceOrientation was deprecated in iOS 8.0
.
启动应用后立即获得设备方向的正确方法是什么?
What is the correct way to get the device orientation right after the app is launched?
推荐答案
我已经对定向进行了很多次测试,所以我总结了一些经验.
I have tested many times about orientation, so I have summed up some experience.
在所有iPhone设备中,除iPhone6s或更高版本外,唯一的接口方向是.portrait
.如果以横向模式启动App,则必须更改方向.一个会收到UIDeviceOrientationDidChangeNotification
.现在是获取方向的合适时间.
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.
关于在iPhone6上横向启动时,启动后的方向将更改一次:
Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:
在使用iPhone6 plus的情况下启动时,启动后的方向从未改变:
The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
使用同一应用,
因此,在应用程序更改方向之前,方向仍然像主页中一样.
So before the app does change orientation, the orientation is still like in the home page.
在viewDidLoad
中,方向尚未更改,日志方向错误.
In viewDidLoad
, the orientation has not changed yet, the log will be the wrong direction.
这篇关于在Swift中,如何在启动后立即正确获取设备方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!