我知道这个问题(或它的变体)已经被问了好几次了,但它仍然是如何最好地处理这个问题,而不是钻研杂乱无章的黑客。
我有一个具有以下布局的应用程序:
UITabBarController
↳ UINavigationController
↳ PortraitViewController
↳ LandscapeViewController
第一个PortraitViewController
将其rightBarButtonItem
设置为一个调用landscapeButtonPressed:
的UIBarButtonItem
。该方法将LandscapeViewController
推送到视图控制器堆栈上。
在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
如您所见,视图未正确旋转。如果我在调用-[UIApplication setStatusBarOrientation:]
之前调用私有-[UIDevice setOrientation:]
方法,我可以让导航栏正确地旋转,但我不希望调用私有方法,而且似乎没有一种方法可以获得用于布局子视图的主视图的边界。使用私有方法会导致如下结果:
Better Landscape View http://img.skitch.com/20091007-8ckbx6gpbiateju9qjgew4x3k2.png
对如何解决这个问题有什么想法吗?
我的目标是拥有一个具有有效的横向CGRect坐标的横向视图,我可以将其用作布局子视图的基础。
发布于 2009-10-09 19:19:18
内森
我觉得你已经把PortraitViewController设置为UINavigationController的rootViewController了。我还相信您在shouldAutorotateToInterfaceOrientation方法中将PortraitViewController的方向限制为仅UIInterfaceOrientationPortrait。如果是这样,则您推送的任何视图控制器都将具有rootViewController本身的方向,除非您不是通过旋转设备来更改设备方向。
因此,如果您需要在UIInterfaceOrientationLandscapeRight方向上使用LandscapeViewController,那么只需要在shouldAutorotateToInterfaceOrientation: method中允许这样做,并且不要通过显式设置设备方向来搞砸事情。
https://stackoverflow.com/questions/1530298
复制相似问题