1

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:^{
    }];
}
amurcia
  • 801
  • 10
  • 26
  • Read this question: http://stackoverflow.com/questions/19041650/ios7-ios6-conditional-rotation-portrait-landscape-for-different-sections-of – Andrey Gordeev Jan 15 '14 at 08:40
  • Regarding the content of the link, I don't understand where I have to push this code: LandscapeViewController * viewController = [[[LandscapeViewController ViewController alloc] init] autorelease]; UINib * nib = [UINib nibWithNibName:@"NavigationController" bundle:nil]; NavigationController *navController = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0]; [navController initWithRootViewController:viewController]; [self presentViewController:navController animated:YES completion:^{ // completion }]; – amurcia Jan 15 '14 at 09:34
  • Have I to create a new ViewController named LandscapeViewController? – amurcia Jan 15 '14 at 09:35
  • No. I think that should be a previous ViewController (the one you're pushing from) – Andrey Gordeev Jan 15 '14 at 10:15
  • Try [this](http://stackoverflow.com/questions/20785766/set-landscape-orientation-for-ipad-only-not-iphone/20786308#20786308) might help you. – Deepak Bharati Jan 15 '14 at 11:52

4 Answers4

1

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

Shubham
  • 570
  • 3
  • 12
  • I tried this, and the error was: `*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'` when I push the landscape viewController – amurcia Jan 15 '14 at 10:49
  • you just need to implement this method in AppDelegate.m and remove any other orientation method from view controller...if it's not working then please send me your block of code. – Shubham Jan 15 '14 at 11:05
  • **AppDelegate.m** `- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }` **AudioViewController.m** `- (void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; }` And I delete other functions related with orientation... – amurcia Jan 15 '14 at 11:10
  • create new demo application and use above function only in AppDelegate.m and after that you can use this to according to your requirement. – Shubham Jan 15 '14 at 11:20
0
-(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.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
0

Try this might help you.

Set Landscape Orientation for iPad Only NOT iPhone

Community
  • 1
  • 1
Deepak Bharati
  • 280
  • 2
  • 13
0

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:^{
    }];
}
amurcia
  • 801
  • 10
  • 26