amurcia
amurcia

Reputation: 791

Objective C only portrait orientation in a viewcontroller

I need to have a ViewController only in portrait orientation. I tried use this code but it didn't work:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL) shouldAutorotate {
    return NO;
}

I tried it too:

AppDelegate.m:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSLog(@"Interface orientations %@", window);
    if(!enablePortrait)
        return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskPortrait;
    return UIInterfaceOrientationMaskPortrait;
}

AudioViewController.m

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
}

it works if initially the iPad was in portrait mode. But if the iPad was in landscape mode, when I go back to the previous viewController (AudioViewController.m) the viewController stays in landscape too.

I tried it too i viewWillAppear.m but no effect:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

Please help me!

Thank you for advance.

EDIT

PROVISIONAL SOLUTION:

Finally I do this: (It works for me, but I hope find a better solution)

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

    UINavigationController* nc = [[UINavigationController alloc] init];
    [self.navigationController presentViewController:nc animated:NO completion:^{
    }];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

Upvotes: 1

Views: 2751

Answers (4)

amurcia
amurcia

Reputation: 791

Finally I do this: (It works for me, but I hope find a better solution)

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

    UINavigationController* nc = [[UINavigationController alloc] init];
    [self.navigationController presentViewController:nc animated:NO completion:^{
    }];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

Upvotes: 0

Deepak Bharati
Deepak Bharati

Reputation: 280

Try this might help you.

Set Landscape Orientation for iPad Only NOT iPhone

Upvotes: 0

Shubham
Shubham

Reputation: 570

use this inside the APPDelegate class for make viewcontroller always in portrait:- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }

Upvotes: 1

Rashad
Rashad

Reputation: 11197

-(BOOL)shouldAutorotate{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
        return YES;
    }
    else {
        return NO;
    }
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And please make your supportedInterfaceOrientations method same. Let me know if that helps.. :)

Upvotes: 0

Related Questions