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

3
我有一个UINavigationController作为我的UIWindow的rootViewController,然后向UINavigationController添加了一个仅支持 Portrait 方向的UIViewController (ControllerA) 作为其rootViewController
稍后,我将UINavigationControllerrootViewController替换为一个新的UIViewController (ControllerB)。ControllerB同时支持 Portrait 和 Landscape 两种方向。我希望我的初始屏幕(ControllerA)只能在Portrait中使用,而应用程序的其余部分可以支持Portrait和Landscape两种方向。
[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] orientation 我所知道的不可靠。 - HalR
当我将ControllerB设置为NavBar的根视图并且我将其保持在横向模式时,为什么状态栏报告处于纵向模式? - Willam Hill
那是一个非常好的问题! - HalR
一个流行的解决方法似乎是转换视图:http://stackoverflow.com/questions/13002477/ios-presenting-a-view-controller-in-landscape-right-from-a-view-controller-supp?rq=1 但我不确定是否有更好且不那么hack的解决方案可供使用。 - Willam Hill
我已经长时间研究了这个问题。据我所知,没有办法确定方向并将其旋转到那里。 - Kevin
1个回答

0

这里提供最简单的解决方案(仅适用于iOS7),但可扩展至支持iOS5。

只需编写自己的UINavigationController(或编写一个类别)并实现supportedInterfaceOrientations方法即可。

@implementation MyNavigationController

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

@end

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接