Allow video on landscape with only-portrait app

2019-01-21 16:21发布

I have a UIWebView included in a UIViewController which is a descendant of UINavigationController. It looks like this:

Main view

The app is portrait only. When I play the video I want the user to be able to rotate the device and see the video in landscape mode. I use this code to allow it:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    id presentedViewController = [self topMostController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

    if ([className isEqualToString:@"MPInlineVideoFullscreenViewController"] ||
        [className isEqualToString:@"MPMoviePlayerViewController"] ||
        [className isEqualToString:@"AVFullScreenViewController"]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController *)topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

And then in my UINavigationController (so when the video finishes the view is not presented in landscape but only in portrait):

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

Everything works perfectly:

Video portrait Video landscape

But then the video is done playing (or the user taps ‘Done’) and the screens return to the underlying view, this is what happens:

Navigation bar issue

As you can see, the navigation bar slips under the status bar. Additionally, I get a lot of auto-layout errors in the logs: http://pastebin.com/09xHzmgJ

Any idea about how to solve this?

8条回答
相关推荐>>
2楼-- · 2019-01-21 17:20

I faced this problem yesterday, where @entropid answer worked for iOS 9 and below, but for iOS 10 it didn't (since iOS 10 did actually hide the status bar, where on iOS 9 and below it was just the UINavigationBar that changed its frame without hiding the status bar and, thus, it overlapped that bar).

Also, subscribing to MPMoviePlayerControllerDidExitFullScreen notification didn't work either, sometimes it simply wasn't called (in my particular case, it was because it was a video from a UIWebView, which used a different class of player which looks similar to MPMoviePlayerController).

So I solved it using a solution like the one @StanislavPankevich suggested, but I subscribed to notifications when a UIWindow has become hidden (which can be in several cases, like when a video has finished, but also when a UIActivityViewController dismisses and other cases) instead of viewWillLayoutSubviews. For my particular case (a subclass of UINavigationController), methods like viewDidAppear, viewWillAppear, etc. simply weren't being called.

viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoDidExitFullscreen:)
                                                 name:UIWindowDidBecomeHiddenNotification
                                               object:nil];

    // Some other logic...
}

dealloc

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

And finally, videoDidExitFullscreen

- (void)videoDidExitFullscreen:(NSNotification *)notification {
    // You would want to check here if the window dismissed was actually a video one or not.

    [[UIApplication sharedApplication] setStatusBarHidden:NO
                                            withAnimation:UIStatusBarAnimationFade];

}

I used UIStatusBarAnimationFade because it looks a lot smoother than UIStatusBarAnimationNone, at least from a user perspective.

查看更多
Root(大扎)
3楼-- · 2019-01-21 17:22

It's very simple, as it says @Stanislav Pankevich, but in

swift 3 version

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();
    UIApplication.shared.isStatusBarHidden = false
}
查看更多
登录 后发表回答