7

按照标题。 调用[[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications]不起作用。UIDevice currentDevice的“方向”始终为空

DidRotateToInterfaceOrientation等事件工作正常,但我需要能够任意调查设备方向。

如何修复/执行此操作?

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

回答

0

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

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

+0

看起来像你没看过这个问题。他说即使他调用beginGeneratingOrientationNotifications,该方法仍然返回NULL。 – Jasarien 2010-05-23 21:29:40

2

似乎是一个愚蠢的问题,不过,是不是

beginGeneratingDeviceOrientationNotifications

(小写B) ...

9

方向的UIDevice的概念似乎是唯一可用在实际设备上。模拟器似乎总是在这里返回0,无论通知是否按照文档提示启用。刺激不方便,但你去了。

我觉得这是正常工作的实际设备上:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]); 
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
+1

是的,只是浪费了2h试图找出什么是错的,但它确实只是模拟器。所有在设备上工作正常。 – samvermette 2010-11-12 02:56:21

+3

[[UIDevice currentDevice]方向]在用户从Springboard锁定设备方向时不起作用。方向始终是最后一个值,并且没有更改通知。 – Jeff 2011-07-16 01:37:53

+5

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication] .statusBarOrientation; 这个替代方案为我工作 – iMeMyself 2012-12-11 11:55:51

1

如果检查[[UIDevice currentDevice] orientation]- (void)viewDidLoad你总是会得到零。

检查它在* - (void) viewDidAppear:(BOOL) *动画的方法和你

+1

“...你总是会得到'nil'”。这是记录的吗? – 2014-02-05 16:54:27

1

这是因为每iMeMyself在上面的评论说 - 这个SAMED了我很多时间,我认为是正确的答案,所以我想强调它在这里:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) 
{ 
    //do stuff here 
} 
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
{ 
//or do stuff here 
} 
+0

这对我有效,但不应该是'UIInterfaceOrientation interfaceOrientation = [....'? – Jimmery 2013-08-13 09:50:06

+0

针对以上使用情况进行了更新 – craigk 2013-08-14 04:03:32