Nathan de Vries
Nathan de Vries

Reputation: 15511

Showing a landscape UIViewController in a portrait UITabBarController/UINavigationController

I understand that this question (or variations of it) has been asked quite a few times, however it's still how to best approach this problem without delving into kludgy hacks.

I have an application with the following layout:

UITabBarController
  ↳ UINavigationController
      ↳ PortraitViewController
          ↳ LandscapeViewController

The first PortraitViewController has its rightBarButtonItem set to a UIBarButtonItem, which calls landscapeButtonPressed:. That method pushes the LandscapeViewController onto the view controller stack.

In LandscapeViewController, I set hidesBottomBarWhenPushed to YES during initialisation, since I only want the navigation bar visible.

I also call setStatusBarOrientation:UIInterfaceOrientationLandscapeRight on [UIApplication sharedApplication] in loadView and viewWillAppear:, and then set it back in to UIInterfaceOrientationPortrait in viewWillDisappear:.

In my shouldAutorotateToInterfaceOrientation: implementation, I return YES only for UIInterfaceOrientationLandscapeRight.

The PortraitViewController looks like this:

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

The LandscapeViewController looks like this:

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

As you can see, the view is not rotating correctly. If I call the private -[UIDevice setOrientation:] method before I call -[UIApplication setStatusBarOrientation:], I can get the navigation bar to rotate properly, but I'd rather not be calling private methods and there doesn't seem to be a way to get the bounds of my main view for laying out subviews. Using the private method results in this:

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

Any ideas on how to solve this problem?

My goal is to have a view in landscape orientation, with valid landscape CGRect coordinates that I can use as the basis for laying out subviews.

Upvotes: 4

Views: 1705

Answers (2)

Shreekara
Shreekara

Reputation: 149

Nathan,

I feel that you have set PortraitViewController as the rootViewController of UINavigationController. I also believe that you are restricting the PortraitViewController's orientation only to UIInterfaceOrientationPortrait only in the shouldAutorotateToInterfaceOrientation method. If so then, any view controller that you push will have the orientation of the rootViewController itself unless you are not changing the device orientation by rotating your device.

So if you need the LandscapeViewController in the UIInterfaceOrientationLandscapeRight orientation then just allow this in shouldAutorotateToInterfaceOrientation: method and don't screw things by explicitly setting the device orientation.

Upvotes: 1

Marcio
Marcio

Reputation: 2107

The LandscapeViewController is not automatically rotating because interfaceOrientation is UIInterfaceOrientationLandscapeRight, so you have to return YES for UIInterfaceOrientationLandscapeRight

To have your views resizing correctly you have to open your xib file with Interface Builder, then into the Inspector and finally into the tab "Size".

Upvotes: 0

Related Questions