在肖像UITabBarController / UINavigationController中显示风景UIViewController

时间:2009-10-07 08:34:46

标签: iphone cocoa-touch uikit

我理解这个问题(或其中的变体)已被问过很多次,但是如果不深入研究kludgy hacks,它仍然是如何最好地解决这个问题。

我有一个具有以下布局的应用程序:

UITabBarController
  ↳ UINavigationController
      ↳ PortraitViewController
          ↳ LandscapeViewController

第一个PortraitViewControllerrightBarButtonItem设置为UIBarButtonItem,调用landscapeButtonPressed:。该方法将LandscapeViewController推送到视图控制器堆栈。

LandscapeViewController中,我在初始化期间将hidesBottomBarWhenPushed设置为YES,因为我只希望导航栏可见。

我也会在setStatusBarOrientation:UIInterfaceOrientationLandscapeRight[UIApplication sharedApplication]的{​​{1}}上致电loadView,然后将其重新设置为viewWillAppear:中的UIInterfaceOrientationPortrait。< / p>

在我的viewWillDisappear:实施中,我仅针对shouldAutorotateToInterfaceOrientation:返回“是”。

UIInterfaceOrientationLandscapeRight看起来像这样:

Portrait View http://img.skitch.com/20091007-18ur7p3iubkrb1if5cak8wdxkb.png

PortraitViewController看起来像这样:

Broken Landscape View http://img.skitch.com/20091007-f3ki1ga5m4ytkyg3wgwektp86e.png

如您所见,视图无法正确旋转。如果我在调用LandscapeViewController之前调用私有-[UIDevice setOrientation:]方法,我可以让导航栏正确旋转,但我宁愿不调用私有方法,似乎没有办法获取我的主视图的边界来布局子视图。使用私有方法导致:

Better Landscape View http://img.skitch.com/20091007-8ckbx6gpbiateju9qjgew4x3k2.png

关于如何解决这个问题的任何想法?

我的目标是以横向方向显示视图,使用有效的横向CGRect坐标作为布局子视图的基础。

2 个答案:

答案 0 :(得分:1)

森,

我觉得你已经将PortraitViewController设置为UINavigationController的rootViewController。我也相信你只是在shouldAutorotateToInterfaceOrientation方法中将PortraitViewController的方向限制为UIInterfaceOrientationPortrait。如果是这样,那么您推送的任何视图控制器都将具有rootViewController本身的方向,除非您没有通过旋转设备来更改设备方向。

因此,如果你需要在UIInterfaceOrientationLandscapeRight方向中使用LandscapeViewController,那么只需在shouldAutorotateToInterfaceOrientation:方法中允许它,并且不要通过明确设置设备方向来搞砸。

答案 1 :(得分:0)

由于interfaceOrientationUIInterfaceOrientationLandscapeRight,因此LandscapeViewController不会自动轮换,因此您必须为UIInterfaceOrientationLandscapeRight

返回YES

要正确调整视图大小,您必须使用Interface Builder打开xib文件,然后打开Inspector,最后打开“Size”选项卡。