UIDevice currentDevice 的“方向”始终为空
正如标题所示。 调用 [[UIDevice currentDevice] BeginGenerateDeviceOrientationNotifications] 没有任何效果。
DidRotateToInterfaceOrientation 等事件工作正常,但我需要能够任意轮询设备方向。
我该如何修复/执行此操作?
长话短说: 我有一个选项卡应用程序,每个选项卡上都有一个导航控制器。第一个选项卡的根视图是一个图表,当方向更改为横向时,该图表会全屏显示;然而,每当视图出现时都需要检查这一点,因为方向变化可能发生在其他地方,所以我希望每当此视图出现时轮询方向状态。
As per the title.
Calling [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] has no effect.
DidRotateToInterfaceOrientation etc events are working fine, but I need to be able to poll the device orientation arbitrarily.
How can I fix/do this?
The long story:
I have a tab application with a navigation controller on each tab. The root view of tab number one is a graph that goes full screen when the orientation changes to landscape; however this needs to be checked whenever the view appears as the orientation change could have occurred elsewhere, so I was hoping to poll the orientation state whenever this view appears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
UIDevice 的方向概念似乎仅适用于实际设备。无论是否按照文档建议启用了通知,模拟器似乎总是在此处返回 0。非常不方便,但就这样吧。
我发现这在实际设备上运行良好:
UIDevice's notion of orientation seems to be only available on actual devices. The simulator seems to always return 0 here, regardless of whether the notifications have been enabled as the docs suggest. Irritatingly inconvenient, but there you go.
I find this works fine on the actual device:
似乎是一个愚蠢的问题,但不是吗
(小写 b )
...
seems like a silly question, but isn't it
( lower case b )
...
如果您在
- (void)viewDidLoad
中检查[[UIDevice currentDevice]orientation]
,您将始终得到 nil。在 *
- (void) viewDidAppear:(BOOL)
* 动画方法中检查它,然后您If you check
[[UIDevice currentDevice] orientation]
in- (void)viewDidLoad
you will always get nil.Check it in *
- (void) viewDidAppear:(BOOL)
*animated method and you这是按照 iMeMyself 在上面的评论中所说的 - 这让我花了很多时间,我认为这是正确的答案,所以我想在这里强调它:
this is as per iMeMyself said in the comments above - this samed me a lot of time and I think is the right answer so I wanted to highlight it here:
[[UIDevice currentDevice]orientation] 不会给你当前的方向状态吗?您可以在想要轮询的视图控制器的 viewWillAppear 方法中检查这一点。
编辑:除此之外,还有多种方法可以获取当前方向,例如使用 UIApplication 中的 statusBarOrientation 属性,或 UIViewcontroller 中的 interfaceOrientation 属性。
Wouldn't [[UIDevice currentDevice] orientation] give you the current orientation state? You could check for this in your viewWillAppear method of the view controller that wants to poll.
Edit: Other than that, there are various ways to get the current orientation, such as using the statusBarOrientation property in UIApplication, or interfaceOrientation property in UIViewcontroller.