Search code examples
iphoneobjective-ccocoa-touchuiwebviewuiappearance

Rotation and appearance of movie player in UIWebView


My application only supports portrait orientation but I have web views in which I display a YouTube video and when this video is played in full screen, I would like the player to be able to go into landscape. How can I support landscape for the video only?

Also, I use the appearance API to set the style of the navigation bar and the bar button items throughout the entire application. This is applied to the movie player as well. How can I use the default layout of the movie player while still using the appearance API in other parts of the application?


Solution

  • I have managed to solve both of the problems.

    For the rotation, enable the landscape orientations in the summary of the target.

    enter image description here

    Then add the following lines to your view controllers. It should be enough to add it to the topmost controller. For example, if your entire application runs in a navigation controller, you can subclass the navigation controller.

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (BOOL)shouldAutorotate
    {
        return NO;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    }
    

    Technically, I think it would be enough to just have shouldAutorotate returning NO as it wouldn't rotate on iOS 6 then and if you don't implement shouldAutorotateToInterfaceOrientation: I believe it defaults to the above for iOS 5. I like to include it anyways.

    This will make sure that your app only works in portrait but this won't have any effect on the MPAVController (which the UIWebView uses to present videos in) and therefore it will rotate correctly.

    To avoid setting the appearance on the MPAVController, I changed my calls to the appearance API from, for example:

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"MyBackgroundImage.png"] forBarMetrics:UIBarMetricsDefault];
    

    To

    [[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil] setBackgroundImage:[UIImage imageNamed:@"NavigationBar_Background.png"] forBarMetrics:UIBarMetricsDefault];
    

    Thereby the background will only be set if the navigation bar is contained within a navigation controller, which it isn't in the MPAVController.