UINavigationController堆栈中支持不同的接口方向



我有一个UINavigationController作为UIWindow的rootViewController,一个仅支持纵向的UIViewControllerControllerA)作为其rootViewController添加到UINavigationController中。

稍后,我将用新的UIViewController(ControllerB)替换UINavigationController's rootViewController。ControllerB同时支持纵向和横向。我希望我的初始屏幕(ControllerA)只能在Portrait中工作,而应用程序的其他部分可以同时支持Portrait和Landscape。

[self.navigationController setViewControllers:@[viewControllerB] animated:NO];
| UINavigationController launches:
| ----ControllerA (rootViewController): Portrait only
| ----ControllerB (rootViewController): Portrait and Landscape supported

如果我在不由ControllerA处理的Landscape中启动我的应用程序,然后转到ControllerB,我的内容(+状态栏)仍在Portrait中。如果我在这一点上手动旋转设备,我就有了正确的内容布局。

如何使ControllerB按照设备的方向进行渲染?

以下是我从ControllerA和ControllerB视图中看到的WillAppear方法:

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(或编写一个类别)并实现支持的InterfaceOrientations。

@implementation MyNavigationController
- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}
@end

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

最新更新