这是一个最重要的观察,因为我似乎找到了解决方法......
当下面的代码位于已推送到 UINavigationController 堆栈的 Controller 中时,它无法工作。在这种情况下,[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.
}
...每次都有效。
尽管如此,我还是不明白为什么第一个变体在推送 View Controller 的上下文中不起作用。有人有想法吗?这只是一个错误吗?
最佳答案
我可以在这里想到一些事情,但我不确定它们是您正在寻找的......
首先,当应用启动时,出于性能原因,设备方向通常会返回 UIDeviceOrientationUnknown
。您可以尝试的一件事是在 .nib 或 Storyboard 中布局 subview ,并使用正确的自动调整大小选项,然后让 iOS 完成它的工作。
其次,如果您像我一样,也许您正在计算机旁边、放在 table 上的设备上进行测试。好吧,在这种情况下,您将不会得到UIDeviceOrientationUnknown
的东西。您实际上应该测试 UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice]orientation])
。例如,如果设备仰卧,它会对该语句返回"is"。这也意味着当您使用 UIInterfaceOrientationIsLandscape(orientation) 时,您会因错误的原因而获得正确的结果。 UIInterfaceOrientationIsLandscape
返回 false 不是因为设备的方向是 UIDeviceOrientationPortrait
,而是因为它无效。
不太确定这是否有帮助,但我花了一段时间才弄清楚这一点,我认为分享此信息会很好! :)
关于iphone - [UIDevice currentDevice].orientation == UIDeviceOrientationUnknown 跟随 UINavigationController 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8486006/