首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >[UIDevice currentDevice].orientation == UIDeviceOrientationUnknown

[UIDevice currentDevice].orientation == UIDeviceOrientationUnknown
EN

Stack Overflow用户
提问于 2011-12-13 08:04:47
回答 1查看 5.6K关注 0票数 7

这是一个比任何事情都重要的观察,因为我似乎找到了一个工作.

下面的代码在被推送到UINavigationController堆栈的控制器中时无法工作。在这种情况下,[UIDevice currentDevice].orientation始终返回UIDeviceOrientationUnknown

代码语言:javascript
复制
-(void)layoutAccordingToOrientation {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    NSLog(@"layoutAccordingToOrientation/orientation==%i", orientation);
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        :
        _detailToolbar.frame = CGRectMake(0, 660, 1024, 44);
    } else {
        :
        _detailToolbar.frame = CGRectMake(0, 916, 768, 44);
    }
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self layoutAccordingToOrientation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

已发出以下呼吁:

代码语言:javascript
复制
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

为了解决这个UIDeviceOrientationUnknown-problem问题,我现在使用以下方法:

代码语言:javascript
复制
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
    etc.
} else {
    etc.
}

..。每次都管用。

尽管如此,我还是不明白为什么第一个变体不能在推送视图控制器的上下文中工作。有什么想法吗?这只是个窃听器吗?

EN

回答 1

Stack Overflow用户

发布于 2012-08-16 22:03:41

我能想到几件事,但我不确定这就是你要找的.

首先,当应用程序启动时,通常由于性能原因,设备方向返回UIDeviceOrientationUnknown。您可以尝试的一件事是使用正确的自动调整选项将您的子视图布局到.nib或Storyboard中,并让iOS完成它的任务。

第二,如果你和我一样,也许你在电脑旁边的一个设备上测试,躺在桌子上。那么,在这种情况下,您是而不是,会得到UIDeviceOrientationUnknown的东西。您应该实际测试UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation])。例如,如果该设备躺在它的背上,它将返回是该语句。这也意味着当您使用UIInterfaceOrientationIsLandscape(orientation)时,由于错误的原因,您得到了正确的结果。UIInterfaceOrientationIsLandscape返回false不是因为设备的方向是UIDeviceOrientationPortrait,而是因为它是无效的。

不太确定这是否有帮助,但我花了一段时间才弄清楚,我想分享这个信息是很好的!:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8486006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档