Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autorotate in iOS 6 has strange behaviour

I have UITabBarController app which plays video and shows other information in other UITabBar tabs. In iOS 6 UIView rotation methods have been deprecated, and now I need to use shouldAutoRotate and supportedInterfaceOrientations methods. For video playing I use MPMoviePlayerViewController.

How to rotate only this player view? I can only rotate whole app, but don't want to do this. I present MPMoviePlayerViewController but it doesn't rotate as in iOS 5 and earlier.

In plist setting I've set only 1 Portrait interface orientation. If I set other - whole app will be rotated.

like image 508
Timur Mustafaev Avatar asked Sep 21 '12 07:09

Timur Mustafaev


5 Answers

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. I have an example here:

https://stackoverflow.com/a/12522119/1575017

like image 122
rocky Avatar answered Oct 18 '22 01:10

rocky


Ough! A half of a day spent, and the problem solved! He he.

As the documentation above says, this is really it! The core points are:

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.

So, any time something with root controller changes, the system asks app delegate "So, what are we? Rotating or not?"

If "rotating":

the supported orientations are retrieved only if this view controller returns YES from its shouldAutorotate method

then system asks our app delegate for

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    

    return ...;
}

That's really pretty simple.

How to determine when should we allow Portrait or Landscape etc - is up to you. Testing for root controller didn't work for me because of some points, but this works:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    

    return self.fullScreenVideoIsPlaying ?
        UIInterfaceOrientationMaskAllButUpsideDown :
        UIInterfaceOrientationMaskPortrait;
}

The property "fullScreenVideoIsPlaying" is set up by me manually whenever I need that.

The only other important thing to be careful is the enum. As it says in docs ... (read carefully above iPad/iPhone thing). So, you can play with those as you need.

Another tiny thing was some buggy behaviour after closing the player controller. There was one time when it did not change the orientation, but that happened once and in some strange way, and only in simulator (iOS 6 only, of course). So I even could not react, as it happened unexpectedly and after clicking fast on some other elements of my app, it rotated to normal orientation. So, not sure - might be some delay in simulator work or something (or, really a bug :) ).

Good luck!

like image 38
Agat Avatar answered Oct 18 '22 03:10

Agat


I had the same problem with my app.

How the rotation in iOS 6 work is that.

=> when ever you are using UINavigationCOntroller the method in AppDelegate

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

decides whether to rotate or not.

=> when the view is presented in the Modal presentation style the method

- (BOOL)shouldAutorotate

which is inside the viewController for that view triggers the method in the appDelegate. And as 1st case appDelegate decides to rotate or not.

My Solution::

What I did for Modal presentation was that. Created a flag in app delegate.

when ever the flag is YES it rotates to Landscape and else its only Portrait.

- (NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    if(self.shouldRotate ) //shouldRotate is my flag
    {
        self.shouldRotate = NO;
        return (UIInterfaceOrientationMaskAll);
    }
    return (UIInterfaceOrientationMaskPortrait);
}

And to toggle between rotations

- (BOOL)shouldAutorotate
{
    YourAppDelegate *mainDelegate = (YourAppDelegate*)[[UIApplication sharedApplication]delegate];
    mainDelegate.shouldRotate = YES;

    return YES;
}

Note: This works is only for view that are Modely Presented. Using Flag, not a good coding practice.

like image 7
Vipin Johney Avatar answered Oct 18 '22 03:10

Vipin Johney


You can also subclass UITabBarController to make it ask its children about the shouldAutorotate and supportedInterfaceOrientation like this:

@implementation MyTabBarController

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

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

@end

Then you just use your custom container in place of the standard one and it works! just tested.

like image 3
Serge Kutny Avatar answered Oct 18 '22 02:10

Serge Kutny


Unfortunately you'll need to turn on all orientations in your plist and use supportedInterfaceOrientations on all the view controllers you don't want to rotate. (In your case everything but the video player.)

like image 2
Anthony Avatar answered Oct 18 '22 03:10

Anthony