Autorotation, UIWebView and UITabBarController

2019-05-29 01:48发布

问题:

I have the following View hierarchy:

UITabBarController
 |
 UINavigationController
 |         |
 |         UIViewController (only supports Portrait rotation)
 |
 UINavigationController
 |         |
 |         UIViewController (only supports Portrait rotation)
 |
 UINavigationController
 |         |
 |         UIViewController (only supports Portrait rotation)
 |         |
 |         UIViewController (has UIWebView with movie in it)
 |
 UINavigationController
           |
           UIViewController (only supports Portrait rotation)

The issue is now that when I display the UIWebView with the movie in it and the user presses "play" the fullscreen media player opens as expected. However, I am not able to rotate the movie since the parent UIViewController only supports Portrait orientation (I tested this). When I enable landscape for the parent view controller it works, yet in this case I run into issues with the user switching tabs whilst still being in landscape mode (see also this question I posted yesterday: Autorotate ignored when changing tabs

Does anybody have any ideas as to how to go about this? On one hand I want to give the user the ability to rotate the video, yet on the other rewriting all the other viewControllers to support landscape as well seems like too much effort for the advantage.

回答1:

Consider a different approach: rather than presenting your UIWebView with the movie in it as yet an UIViewController in your whole tab controller heirarchy, which ties you into the whole rotation problem, try replacing your top level view controller (UITabBarController) with a totally different UIViewController (containing the UIWebView).

When you want to exit the fullscreen movie mode, reinstate the UITabBarController as the top level view controller again. N.B. do not forget to remove the 'old' view controller when you do a switch -- UIWindow gets very unhappy when it has multiple child views, and autorotation messages don't get sent, etc.

I've seen apps use strategies like this when faced with such autorotation hurdles.

This strategy might seem a bit 'unglued', and you might have to do a little fiddling around the finer aspects of the switching, but it's worth a look.



回答2:

You can send a notification in viewcontrollers to say other view controllers that rotation is required so that they return YES to all orientations.When you leave the view controllers you send another notifications that it is no longer required.With that approach you could have autorotation only in the views that you want and as soon as you leave these views you deactivate it.

In my app i am using this approach in a web view where auto rotation is required but not in other view controllers.

  • (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    NSNotification *autoRotationNotification = [NSNotification notificationWithName:kShouldAutoRotateNotification object:[NSNumber numberWithBool:YES]]; [[NSNotificationCenter defaultCenter] postNotification:autoRotationNotification];

}

  • (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated];

    NSNotification *autoRotationNotification = [NSNotification notificationWithName:kShouldAutoRotateNotification object:[NSNumber numberWithBool:NO]]; [[NSNotificationCenter defaultCenter] postNotification:autoRotationNotification]; }