UIDevice currentDevice的"orientation"始终为null

Tob*_*ter 8 iphone objective-c orientation uidevice interface-orientation

根据标题.调用[[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications]无效.

DidRotateToInterfaceOrientation等事件工作正常,但我需要能够任意轮询设备方向.

我该如何修复/做到这一点?

长话故事:我在每个选项卡上都有一个带有导航控制器的选项卡应用程序.第一个选项卡的根视图是当方向变为横向时全屏显示的图形; 然而,每当视图出现时都需要检查,因为方向改变可能在其他地方发生,所以我希望在出现此视图时轮询方向状态.

Ben*_*tto 9

UIDevice的定位概念似乎只能在实际设备上使用.无论是否已按照文档建议启用通知,模拟器似乎总是在此处返回0.非常不方便,但你去了.

我发现这在实际设备上工作正常:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)

  • UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication] .statusBarOrientation; 这个替代方案对我有用 (5认同)
  • 如果用户已将设备方向锁定在Springboard上,则[[UIDevice currentDevice] orientation]不起作用.orientation始终是最后一个值,并且没有更改通知. (4认同)

Dav*_*Liu 0

[[UIDevice currentDevice]orientation] 不会给你当前的方向状态吗?您可以在想要轮询的视图控制器的 viewWillAppear 方法中检查这一点。

编辑:除此之外,还有多种方法可以获取当前方向,例如使用 UIApplication 中的 statusBarOrientation 属性,或 UIViewcontroller 中的 interfaceOrientation 属性。