Autorotate in iOS 6 has strange behaviour

2019-01-04 00:43发布

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.

7条回答
孤傲高冷的网名
2楼-- · 2019-01-04 01:18

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

查看更多
老娘就宠你
3楼-- · 2019-01-04 01:18

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.)

查看更多
Luminary・发光体
4楼-- · 2019-01-04 01:19

I found the easiest way to set this up is to use the "supported interface orientations" buttons which you can see if you look at Targets....Summary Tab (under iPhone/iPad Deployment info).

Its basically a GUI to set the plist file

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-04 01:24

Try this,

If TabBarController is RootViewController for window, Then Create Custom Class which inherits TabBarController say CustomTabBarController.h

Add Below method in CustomTabBarController.h

-(NSUInteger)supportedInterfaceOrientations // Must return Orientation Mask

Finally Call Below in AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {
   if( [self.window.rootViewController supportedInterfaceOrientations]!=0) 
     {
        return [self.window.rootViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskAll;
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 01:26

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.

查看更多
够拽才男人
7楼-- · 2019-01-04 01:27

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!

查看更多
登录 后发表回答