如何通过UIView参考获取设备方向?

the*_*ion 5 uiview uiinterfaceorientation ios

我需要从ViewController获取设备方向.我不能依据:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
Run Code Online (Sandbox Code Playgroud)

因为它有时会返回未知方向(例如,当设备放在桌子上时).

我只需知道我当前的UIView显示的方向(左侧是横向还是横向右侧).当方向改变时,我不需要更新此值,我只想知道它,当我要求它时.只是一些参考view.orientation ;).有什么东西可以帮助我吗?我已经阅读了UIView文档,发现了一些对UIWindow的引用,但没有什么可以帮助我.

vis*_*shy 13

如果您尝试使用,还可以从UIApplication获取设备方向

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
Run Code Online (Sandbox Code Playgroud)


ste*_*veb 5

UIViewController 有一个属性

UIInterfaceOrientation interfaceOrientation;
Run Code Online (Sandbox Code Playgroud)

因此,在您的 UIViewController 中,您可以随时通过以下方式访问设备的当前方向

UIInterfaceOrientation myCurrentOrientation = self.interfaceOrientation;
Run Code Online (Sandbox Code Playgroud)