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:

(source: pessoal.org) (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?

Answers


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


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);
}

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


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.


Need Your Help

@PropertySource in a Jar for an external file on the classpath

java spring jar classpath properties-file

I'm trying to use the Spring framework's @PropertySource annotation in a Jar to load a properties file from outside the jar, but it's not finding the file.

Typing Text Script with unordered list

javascript jquery html unordered

I have been using the typewriter.js effect for a while. Works great. now I want to use it with a unodered list (ul), but if I use any tags, the effect does not want to work. Any help?