iphone - [UIDevice currentDevice].orientation == UIDeviceOrientationUnknown 跟随 UINavigationController 推送

标签 iphone uinavigationcontroller uiinterfaceorientation uideviceorientation

这是一个最重要的观察,因为我似乎找到了解决方法......

当下面的代码位于已推送到 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/

相关文章:

iphone - iPhone 应用程序中的 Wi-Fi 强度

ios - 运行 Non-Storyboard 应用程序的运行时错误

ios - UIModalPresentationCurrentContext 和 willRotateToInterfaceOrientation

ios - 在一个模态视图 Controller 上强制 iOS 方向为横向

ios - 为什么我的按钮在横向模式下不显示?

ios - 如何使用 iphone 使用蓝牙或 wifi 与 OBD II 通信

iphone - OpenGL ES 平铺纹理 Mipmap 问题 - iPad/iPhone

swift - ARSCNView 嵌入导航 Controller 时不显示对象

uinavigationcontroller - 从 UINavigationController 转回根 Controller ?

ios - MapKit 还是 Google Maps SDK? (iOS)