20

I am using

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

delegate to change the frame of my view based on the orientation type

i.e.,

if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
    self.view.frame=CGRectMake(0,0,500,300);
}
else
{
    self.view.frame=CGRectMake(0,0,300,400);
}

How to handle the same situation in iOS 6 as

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

has been deprecated in iOS6.

I am using the following delegate to set all the orientations.

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationAllMask;
}

But,

-(BOOL)shouldAutoRotate
{
    return YES;

}

is not getting invoked. How to handle this situation?

enter image description here

Bharath
  • 3,001
  • 6
  • 32
  • 65

7 Answers7

46

In the AppDelegate, I have added the ViewController Object to window as

[self.window addSubView:viewControllerObj]

The problem was with the above line. Orientation will work properly with the above line in iOS 5 but in iOS, for orientation to work properly, change the above line with

[self.window setRootViewController:viewControllerObj]

Then the app rotates when orientation changes.

Bharath
  • 3,001
  • 6
  • 32
  • 65
  • How did you come to know that ? This resolved my issue to. +5 – Janak Nirmal Oct 04 '12 at 13:26
  • +1,partially solved my problem at http://stackoverflow.com/questions/13704218/ios6-autorotation-portaitupsidedown-to-portait I can't handle properly the upside down case –  Dec 04 '12 at 14:45
  • 1
    Only the top most Controller get's notified of the orientation change. If you add the controller as subview the message is not passed to this controller. – Dennis Stritzke Jun 06 '13 at 08:07
3

Make sure the settings in the project and target allow for the orientations for each device type.

Also, the code you have in shouldAutorotateToInterfaceOrientation: you can put in viewDidLayoutSubviews.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • I have also added that settings. Even then, orientation is not changing when I change the device orientation. Why? I have uploaded the info.plist file partial screen shot. Please see.. – Bharath Sep 20 '12 at 19:55
  • 2
    Your answer is partially correct. But, another mistake I made was that I added the viewController as subview to the window. Instead If I set that viewController object as `[self.window setRootViewController:viewControllerObj]` then orientation is working properly! – Bharath Sep 21 '12 at 10:52
  • Ah, good catch. There is a problem with view controllers and windows, where only the root view controller receives orientation change notifications. – Léo Natan Sep 21 '12 at 12:49
1

Remember that in iOS 6 the handling of the rotation is take care in parents view. Less responsability to childs viewcontrollers. But more annoying for us that code everything without Interface builder.

lagos
  • 1,968
  • 3
  • 17
  • 26
  • 6
    Apple did a very bad job in iOS 6 for Orientation. We have to change all the apps to support iOS 6. (Why it's not supporting old way too ) – Bagusflyer Oct 10 '12 at 03:50
  • Totally agree. The worst that I changed everything to support iOS6, and removed the old methods, so when I tested in a old iPad after a while, nothing works. – lagos Oct 10 '12 at 07:49
0

Make sure all you orientation is enable.enter image description here

Gurumoorthy Arumugam
  • 2,129
  • 1
  • 25
  • 40
0

Handling UINavigation orientation issue in iOS 6

1 Create a UINavigation+Rotation category class

2 put below methods in UINavigation+Rotation.m class

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject]supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.viewControllers count] == 0) {
        return UIInterfaceOrientationPortrait;
    }
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
Ricard Pérez del Campo
  • 2,387
  • 1
  • 19
  • 22
0

This method work fine for IOS 6 and Older versions


-(BOOL)rotationChanged:(UIInterfaceOrientation)interfaceOrientation {
    NSInteger orientation = [[UIDevice currentDevice] orientation];
    UIWindow *_window = [[[UIApplication sharedApplication] delegate] window];
    if ([PGPlatformUtils GetCurrentPlatform]==PGPlatformEnum_iPhone) {
        switch (orientation) {
            case 1:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (0)];
                [_window setFrame:CGRectMake(0, 0, 320, 480)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

                [UIView commitAnimations];
                break;
            case 2:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (M_PI)];
                [_window setFrame:CGRectMake(0, 0, 320, 480)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

                [UIView commitAnimations];
                break;
            case 3:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)];
                [_window setFrame:CGRectMake(0, 0, 320, 480)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

                [UIView commitAnimations];
                break;
            case 4:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)];
                [_window setFrame:CGRectMake(0, 0, 320, 480)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

                [UIView commitAnimations];
                break;
            case 5:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (0)];
                [_window setFrame:CGRectMake(0, 0, 320, 480)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIPrintInfoOrientationLandscape animated:YES];

                [UIView commitAnimations];
                break;


            default:
                break;
        }
    }
    else{
        switch (orientation) {
            case 1:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (0)];
                [_window setFrame:CGRectMake(0, 0, 768, 1024)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
                NSLog(@"*** 1 Orientation Call 0");

                [UIView commitAnimations];
                break;
            case 2:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (M_PI)];
                [_window setFrame:CGRectMake(0, 0, 768, 1024)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown animated:YES];
                NSLog(@"*** 1 Orientation Call M_PI");
                [UIView commitAnimations];
                break;
            case 3:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)];
                [_window setFrame:CGRectMake(0, 0, 768, 1024)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
                NSLog(@"*** 1 Orientation Call M_PI/2");
                [UIView commitAnimations];
                break;
            case 4:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)];
                [_window setFrame:CGRectMake(0, 0, 768, 1024)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
                NSLog(@"*** 1 Orientation Call - M_PI/2");
                [UIView commitAnimations];
                break;
            case 5:
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.0];

                [_window setTransform:CGAffineTransformMakeRotation (0)];
                [_window setFrame:CGRectMake(0, 0, 768, 1024)];
                [[UIApplication sharedApplication] setStatusBarOrientation:UIPrintInfoOrientationLandscape animated:YES];
                NSLog(@"*** 1 Orientation Call 0");
                [UIView commitAnimations];
                break;

            default:
                break;
        }
    }
    return YES;

}
Dhwani
  • 7,484
  • 17
  • 78
  • 139
eranda.del
  • 2,775
  • 1
  • 15
  • 11
0

Use willAnimateRotationToInterfaceOrientation in iOS6.

Andrew
  • 11,894
  • 12
  • 69
  • 85