ios - UINavigationController 堆栈中支持的不同界面方向

标签 ios uiviewcontroller

我有一个 UINavigationController 作为我的 UIWindow 的 rootViewController 和一个仅支持纵向方向的 UIViewController (ControllerA) 添加到 UINavigationController 作为它的 rootViewController

稍后,我将 UINavigationController 的 rootViewController 替换为新的 UIViewController (ControllerB)。 ControllerB 支持纵向和横向。我希望我的初始屏幕 (ControllerA) 仅在纵向模式下工作,而应用程序的其余部分可以同时支持纵向和横向。

[self.navigationController setViewControllers:@[viewControllerB] animated:NO];

| UINavigationController launches:
| ----ControllerA (rootViewController): Portrait only
| ----ControllerB (rootViewController): Portrait and Landscape supported

如果我在 ControllerA 未处理的横向模式下启动我的应用程序,然后移动到 ControllerB,我的内容(+状态栏)仍处于纵向模式。如果此时我手动旋转设备,我就有了正确的内容布局。

如何让 ControllerB 以设备的方向呈现自己?

这是我从 ControllerA 和 ControllerB viewWillAppear 方法中看到的内容:

navigationController orientation: UIInterfaceOrientationPortrait 
UIViewController interfaceOrientation: UIInterfaceOrientationPortrait 

<!-----------!!two values are different!!------------------>
[[UIDevice currentDevice] orientation]: UIDeviceOrientationLandscapeLeft 
[[UIApplication sharedApplication] statusBarOrientation]: UIInterfaceOrientationPortrait 
Phone is physically held in Landscape at this point in time.

最佳答案

这是最简单的解决方案(仅限 iOS7),但它可以扩展到适用于 iOS5。

只需编写我们自己的 UINavigationController(或编写一个类别)并实现 supportedInterfaceOrientations。

@implementation MyNavigationController

- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

@end

当您想知道界面方向时,请始终使用 UIViewController.interfaceOrientation。状态栏方向可能是错误的(在多个窗口的情况下)并且设备方向是完全错误的,因为设备可以正面朝上(平放在 table 上)并且您无法从中推断出界面方向。

关于ios - UINavigationController 堆栈中支持的不同界面方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18093016/

相关文章:

ios - 如何制作一个异步方法来处理来自委托(delegate)的数据?

ios - 检索 NSDictionary 对象

iOS 缩放我的网页

ios - 如何检查当前的 ViewController 是否是我要推送的那个?

objective-c - UIViewControllers 问题

ios - iOS:主线程检查器:在后台线程上调用的UI API:-[UIView keepCount]

ios - 改变ios导航栏的高度

cocoa - 在 Cocoa 中创建启动屏幕

ios - 出于测试目的在 UIViewController 上设置显式 screenOrientation

ios - segue完成后如何执行一些代码?