这是一个比任何事情都重要的观察,因为我似乎找到了一个工作.
下面的代码在被推送到UINavigationController堆栈的控制器中时无法工作。在这种情况下,[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-problem问题,我现在使用以下方法:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
etc.
} else {
etc.
}..。每次都管用。
尽管如此,我还是不明白为什么第一个变体不能在推送视图控制器的上下文中工作。有什么想法吗?这只是个窃听器吗?
发布于 2012-08-16 22:03:41
我能想到几件事,但我不确定这就是你要找的.
首先,当应用程序启动时,通常由于性能原因,设备方向返回UIDeviceOrientationUnknown。您可以尝试的一件事是使用正确的自动调整选项将您的子视图布局到.nib或Storyboard中,并让iOS完成它的任务。
第二,如果你和我一样,也许你在电脑旁边的一个设备上测试,躺在桌子上。那么,在这种情况下,您是而不是,会得到UIDeviceOrientationUnknown的东西。您应该实际测试UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation])。例如,如果该设备躺在它的背上,它将返回是该语句。这也意味着当您使用UIInterfaceOrientationIsLandscape(orientation)时,由于错误的原因,您得到了正确的结果。UIInterfaceOrientationIsLandscape返回false不是因为设备的方向是UIDeviceOrientationPortrait,而是因为它是无效的。
不太确定这是否有帮助,但我花了一段时间才弄清楚,我想分享这个信息是很好的!:)
https://stackoverflow.com/questions/8486006
复制相似问题