0

In an app i'm maintaining there's a rotate that should happen in portrait and portraitupsidedown mode. (all the rotation are enabled in the summary panel.)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown;
}

or

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

No matter what I tried I couldn't get the rotate to accure i ios 6

Things I've tried so far:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait; 
}

-(NSInteger)supportedInterfaceOrientations{
NSInteger mask = 0;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
    mask |= UIInterfaceOrientationMaskLandscapeRight;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
    mask |= UIInterfaceOrientationMaskLandscapeLeft;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
    mask |= UIInterfaceOrientationMaskPortrait;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
    mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
return mask;
}

I tried putting this in my appdelegate:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortraitUpsideDown;
}

But I'm getting this error: Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'

Tried putting this in my delegate:

- (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; 
}

I read all the discussions about it and the deprecations of shouldAutorotateToInterfaceOrientation but i still can't get it to work.

I'm about to lose it

Segev
  • 18,651
  • 12
  • 76
  • 148
  • Try to create one category and handle the orientation. Its working fine for me. – Ganapathy Apr 08 '13 at 05:55
  • Can you please provide a way \ some code to do so? – Segev Apr 08 '13 at 05:57
  • remove your methods and add the code and let me know -(BOOL)shouldAutorotate { return YES; } -(NSInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAll; } – Arun Apr 08 '13 at 07:14
  • and also change the app delegate method to this -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskAll; } – Arun Apr 08 '13 at 07:16
  • i still can't get it to work. – Segev Apr 08 '13 at 07:36
  • where is the `–shouldAutorotate` method? – holex Jul 08 '13 at 07:59

4 Answers4

2

From Apple's iOS 6 SDK Release Notes:

Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods.

More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.

A view controller’s supported interface orientations can change over time—even an app’s supported interface orientations can change over time. The system asks the top-most full-screen view controller (typically the root view controller) for its supported interface orientations whenever the device rotates or whenever a view controller is presented with the full-screen modal presentation style. Moreover, the supported orientations are retrieved only if this view controller returns YES from its shouldAutorotate method. The system intersects the view controller’s supported orientations with the app’s supported orientations (as determined by the Info.plist file or the app delegate’s application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.

The system determines whether an orientation is supported by intersecting the value returned by the app’s supportedInterfaceOrientationsForWindow: method with the value returned by the supportedInterfaceOrientations method of the top-most full-screen controller. The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0. This makes the caller responsible for ensuring that the status bar orientation is consistent.

For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.

If you want your whole app to rotate then you should set your Info.plist to support all orientations. Now if you want a specific view to be portrait only you will have to do some sort of subclass and override the autorotation methods to return portrait only. Just have a look at How to force a UIViewController to Portrait orientation in iOS 6

Community
  • 1
  • 1
Vineet Singh
  • 4,793
  • 1
  • 29
  • 39
  • My target summery is set to support all orientations there for my Info.plist is set to support all orientations. – Segev Apr 08 '13 at 05:58
  • I added the methods in your link to my UINavigationController subclass, I can see that the functions are getting called but the screen will stay on portrait mode even if I flip my device – Segev Apr 08 '13 at 06:24
  • oh..is it happening. just wait,i'll modify my answer. – Vineet Singh Apr 08 '13 at 06:37
1

My application targeted from IOS 5. I used shouldAutorotateToInterfaceOrientation method (by default) for IOS 5 device. And categories UINavigationController to handle the orientation for IOS 6.

#import "UINavigationController+Rotation_IOS6.h"

    @implementation UINavigationController (Rotation_IOS6)

    -(BOOL)shouldAutorotate
    {

            return YES;
    }

    -(NSUInteger)supportedInterfaceOrientations
    {

        return UIInterfaceOrientationMaskAll;
    }

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        if([self.visibleViewController isMemberOfClass:NSClassFromString(@"SampleViewController")])
        {
            return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
        }
        return UIInterfaceOrientationPortrait;

    }

    @end
halfer
  • 19,471
  • 17
  • 87
  • 173
Ganapathy
  • 4,554
  • 5
  • 21
  • 41
  • I added the above to my UINavigationController sub class, I can see that `supportedInterfaceOrientations` is getting called but the orientation still won't change. – Segev Apr 08 '13 at 06:23
0

Check your target properties...look like below

enter image description here

dhaya
  • 1,532
  • 13
  • 21
0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}
Gank
  • 5,283
  • 4
  • 47
  • 45