7

这比什么都重要,因为我似乎找到了解决方法......

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

-(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;
}

拨打以下电话:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

为了解决这个UIDeviceOrientationUnknown问题,我现在使用以下代码:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
    etc.
} else {
    etc.
}

...每次都有效。

尽管如此,我还是不明白为什么第一个变体在推送视图控制器的上下文中不起作用。任何人的想法?它只是一个错误吗?

4

1 回答 1

3

我可以在这里想到几件事,但我不确定它们是你要找的东西......

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

其次,如果你和我一样,也许你正在电脑旁边的设备上进行测试,放在桌子上。好吧,在那种情况下,你不会得到这个UIDeviceOrientationUnknown东西。您实际上应该测试UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation]). 例如,如果设备仰卧,它会对该语句返回“是”。这也意味着当你使用 时UIInterfaceOrientationIsLandscape(orientation),你会因为错误的原因得到正确的结果。UIInterfaceOrientationIsLandscape返回 false 不是因为设备的方向是,UIDeviceOrientationPortrait而是因为它无效。

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

于 2012-08-16T22:03:41.217 回答