我知道这个问题(或其变体)已经被问过很多次了,但是它仍然是如何在不深入研究笨拙黑客的情况下最好地解决这个问题。
我有一个具有以下布局的应用程序:
UITabBarController
↳ UINavigationController
↳ PortraitViewController
↳ LandscapeViewController
第一个 PortraitViewController
将其 rightBarButtonItem
设置为 UIBarButtonItem
,它调用 landscapeButtonPressed:
。该方法将 LandscapeViewController
推送到 View Controller 堆栈上。
在 LandscapeViewController
中,我在初始化期间将 hidesBottomBarWhenPushed
设置为 YES
,因为我只希望导航栏可见。
我还在 loadView
和 viewWillAppear:
中的 [UIApplication sharedApplication]
上调用 setStatusBarOrientation:UIInterfaceOrientationLandscapeRight
,然后将其重新设置为 viewWillDisappear:
中的 UIInterfaceOrientationPortrait
。
在我的 shouldAutorotateToInterfaceOrientation:
实现中,我仅针对 UIInterfaceOrientationLandscapeRight
返回 YES。
PortraitViewController
看起来像这样:
Portrait View http://img.skitch.com/20091007-18ur7p3iubkrb1if5cak8wdxkb.png
LandscapeViewController
看起来像这样:
Broken Landscape View http://img.skitch.com/20091007-f3ki1ga5m4ytkyg3wgwektp86e.png
如您所见, View 旋转不正确。如果我在调用 -[UIApplication setStatusBarOrientation:]
之前调用私有(private) -[UIDevice setOrientation:]
方法,我可以让导航栏正确旋转,但我会而不是调用私有(private)方法,并且似乎没有办法获取我的主视图的边界来布局 subview 。使用私有(private)方法会导致以下结果:
Better Landscape View http://img.skitch.com/20091007-8ckbx6gpbiateju9qjgew4x3k2.png
关于如何解决这个问题有什么想法吗?
我的目标是拥有一个横向 View ,并具有有效的横向 CGRect 坐标,我可以将其用作布局 subview 的基础。
最佳答案
内森,
我觉得你已经将PortraitViewController设置为UINavigationController的rootViewController。我还相信您仅在 shouldAutorotateToInterfaceOrientation 方法中将 PortraitViewController 的方向限制为 UIInterfaceOrientationPortrait 。如果是这样,那么您推送的任何 View Controller 都将具有 rootViewController 本身的方向,除非您不通过旋转设备来更改设备方向。
因此,如果您需要 UIInterfaceOrientationLandscapeRight 方向的 LandscapeViewController,那么只需在 shouldAutorotateToInterfaceOrientation: 方法中允许此操作,并且不要通过显式设置设备方向来搞砸事情。
关于iphone - 在纵向 UITabBarController/UINavigationController 中显示横向 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1530298/