0

Alright, since no one answered my previous question, I have come to believe that there may be no easy way to do this. But I am optimistic. Here's my issue:

In my app, I am switching from ViewControllerOne to ViewControllerTwo by using a regular UIButton. ViewControllerOne is always in landscape mode. ViewControllerTwo is supposed to be always in portrait mode. However, when I push the button in my landscape ViewControllerOne, ViewControllerTwo is also in landscape mode, although I want it to switch to portrait mode regardless of how the device is rotated by the user when the button is pressed.

I added the following code to my AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}

And I added this to my ViewController that's supposed to be in portrait mode only:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;

}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

-(void)viewWillAppear:(BOOL)animated
{
    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationPortrait;
}

Is there a way to tell the viewController to go into portrait mode, even if the previous view was landscape? Maybe I could create a custom segue that would force the view to be in portrait mode when the button is pushed? What would be the best/official solution here?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
George Friday
  • 585
  • 1
  • 6
  • 22
  • Now sure why I got downgraded... this is an honest question, and I don't have the solution. Please leave a comment if you downgrade. – George Friday Aug 25 '13 at 02:46
  • possible duplicate of [setStatusBarOrientation:animated: not working in iOS 6](http://stackoverflow.com/questions/12563954/setstatusbarorientationanimated-not-working-in-ios-6) – Caleb Aug 25 '13 at 03:00
  • Could `attemptRotationToDeviceOrientation` be useful to you? – Ben Flynn Aug 25 '13 at 05:32
  • the `–shouldAutorotate` method should return `YES`, if you want to allow the oriantation to be changed in iOS6+. I recommend you to read the Apple Docs of the orientation support, because it is pointless to copy the relevant parts here. – holex Aug 25 '13 at 12:45
  • possible duplicate of [Status bar is Landscape, but \[\[UIApplication sharedApplication\] statusBarOrientation\] returns portrait](http://stackoverflow.com/questions/5810397/status-bar-is-landscape-but-uiapplication-sharedapplication-statusbarorienta) – jlehr Aug 25 '13 at 22:01

2 Answers2

0

Based on my understanding of the question I assume that you want the 2nd view controller to be portrait and the first view controller to be landscape.

For the second viewcontroller, add this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

For the first view controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscape);

}
Coder404
  • 742
  • 2
  • 7
  • 21
0

Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need.

Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). Add the following method in this custom navigation controller (it is for landscape orientation but you can replace it for portrait)

- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

and add to your view controller that should rotate:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. Then, if you want to block some orientations for a particular view:

– application:supportedInterfaceOrientationsForWindow:
Community
  • 1
  • 1
nabrugir
  • 1,839
  • 2
  • 22
  • 38