Manually set interface orientation
Asked Answered
G

3

4

Is there any easy way to manually set the orientation of an interface? I need to set the interface to portrait even though the device orientation might be in landscape during loading. Kinda want to stay away from CGAffineTransforms.

Gnawing answered 31/8, 2011 at 21:57 Comment(0)
H
16

One method I know that works for me (and is a bit of a hack and can display one orientation before changing to the orientation you want) is:

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];

    UIApplication* application = [UIApplication sharedApplication];

    if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
    {
        UIViewController *c = [[UIViewController alloc]init];
        [self presentModalViewController:c animated:NO];
        [self dismissModalViewControllerAnimated:NO];
        [c release];
    }
}
Hemostat answered 31/8, 2011 at 22:5 Comment(3)
This actually works really well for the situation I described. But for some reason does not work well with trying to force landscape from portrait mode(changing UIInterfaceOrientationPortrait to UIInterfaceOrientationLandscapeLeft). Ideas?Gnawing
I have no idea Dan, you will have to try it yourself by making a sub-classed UIViewController that only handles landscape in the 'shouldAutorotateToInterfaceOrientation:' method.Hemostat
@ShanePowell, your code worked great on pre iOS6. Is there any such tricks for iOS6?Senhauser
V
2

override this to control the orientation until loading...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
Viridis answered 31/8, 2011 at 22:7 Comment(1)
Yes, you are right. My mistake. I updated my answer. You can consider just overriding the shouldAutorotateToInterfaceOrientation and maintain the orientation as long as you need.Viridis
R
2

First, set your app and views to only support portrait, then use this category method taken from my refactoring library, es_ios_utils:

@interface UIViewController(ESUtils)
    // Forces without using a private api.
    -(void)forcePortrait;
@end

@implementation UIViewController(ESUtils)

-(void)forcePortrait
{
    //force portrait orientation without private methods.
    UIViewController *c = [[UIViewController alloc] init];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    [c release];
}

@end

The view, dismissed before the frame completes, won't be displayed.

Redhot answered 31/8, 2011 at 22:9 Comment(2)
I probably got this from Shane or whatever his source was. His is a bit more complete, and mine suggests the use of a category for reuse.Redhot
Portrait is the default supported orientation for an unextended UIViewController, and presenting it will force a valid orientation.Redhot

© 2022 - 2024 — McMap. All rights reserved.