【问题标题】:Different supported interface orientations in UINavigationController stackUINavigationController 堆栈中支持的不同界面方向
【发布时间】:2013-08-07 01:06:56
【问题描述】:

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

稍后,我将 UINavigationController's 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.

【问题讨论】:

  • 根据我的经验,[[UIApplication sharedApplication] statusBarOrientation]: UIInterfaceOrientationPortrait 是确定的并且永远是正确的。据我所知,[UIDevice currentDevice] 方向不可靠。
  • 为什么当 ControllerB 设置为 NavBar 的根但我保持横向时,statusBar 报告处于纵向模式?
  • 这是一个非常好的问题!
  • 一种流行的解决方法似乎是转换视图:stackoverflow.com/questions/13002477/…,但我不确定是否有更好、更少被黑的解决方案。
  • 我一直在仔细研究这个问题。据我所知,没有办法确定方向应该是什么并在那里旋转。

标签: ios uiviewcontroller


【解决方案1】:

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

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

@implementation MyNavigationController

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

@end

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

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 2015-12-23
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多