jpm
jpm

Reputation: 16712

How to force a screen orientation in specific view controllers?

My application is pretty simple: it starts up with a view controller that holds a table view (in grouped view layout) with a few options. When the user taps on one of the options, I push another view controller onto my navigation controller.

This second view controller simply displays a UIImageView, and the user can change the screen orientation on this view controller between portrait/landscape modes. This works just fine, and all is happy.

However, if the user taps on the "Back" button on my navigation bar while on the landscape mode, the first controller's layout is all messed up. See below for before/after screenshots:

before
(source: pessoal.org)
after
(source: pessoal.org)

Any clues on how to force the first view controller (second screenshot in this post) to stay within the portrait screen orientation?

Upvotes: 16

Views: 16760

Answers (4)

Vinzius
Vinzius

Reputation: 2901

There is a solution to do that : it's to use a view controller and adding its view to the window. then in that controller you force landscape in the shouldAutorotate... methode. It works fine, but be sure it's necessary for your project to use that, because it's not very smart  to force the user to turn his iPhone. By the way, here is an example code if you need it.

http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html

Upvotes: 0

Reputation:

Like someone on the open radar suggested, a workaround is to disable "back" button while in non-portrait:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    // don't let user press "back" button in landscape - otherwise previous view and the rest of the application
    // will also be in landscape which we did not feel like testing yet
    self.navigationController.navigationBarHidden = (UIInterfaceOrientationPortrait != self.interfaceOrientation);
}

Upvotes: 0

lfalin
lfalin

Reputation: 4304

There does not appear to be a way to do this using the documented methods.

I have filed a bug for this: rdar://6399924

"There is no way to always restrict a UIViewController to one orientation"

You can see it on open radar (along with a link to sample code to reproduce the problem) here: http://openradar.appspot.com/radar?id=697

Upvotes: 2

Mark Bessey
Mark Bessey

Reputation: 19782

I wasn't able to get this to work the way I wanted. You ought to be able to set a particular orientation for a ViewController, but the NavigationController doesn't seem to always do the right thing.

I ennded up re-designing my screens so that they all work in either orientation. That might be extra work, but it "feels" more natural, anyway.

Upvotes: -1

Related Questions