Allow video on landscape with only-portrait app

前端 未结 8 1348
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 06:35

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

相关标签:
8条回答
  • 2020-12-13 07:18

    On iOS 11 accepted solution didn't work for me. Seems that navigation bar stops reflecting for frame changes. But there is a workaround. At first, we need to modify supportedInterfaceOrientationsForWindow method to return UIInterfaceOrientationMaskLandscape for video controllers instead of UIInterfaceOrientationMaskAllButUpsideDown. In my case when I tap on embedded YouTube video, system always opens AVFullScreenViewController, so I removed other checks from original example. Code:

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        __kindof UIViewController *presentedViewController = [self topMostController];
    
        // Allow rotate videos
        NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
        if ([className isEqualToString:@"AVFullScreenViewController"]) {
            return UIInterfaceOrientationMaskLandscape;
        }
    
        return UIInterfaceOrientationMaskPortrait;
    }
    

    This didn't changes behaviour of AVFullScreenViewController on iOS 10 and less, but fixes navigation bar on iOS 11, so there is no need to update frame (also there is a side-effect on iOS 11 that video rotates from landscape when starts playing, but it's a tradeoff). Next, we need to add check in UIWindowDidBecomeHiddenNotification method:

    - (void)videoDidExitFullscreen {
        if (@available(iOS 11, *)) {
            // Fixes status bar on iPhone X
            [self setNeedsStatusBarAppearanceUpdate];
        } else {
            self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, statusAndNavBarHeight);
        }
    }
    

    Without setNeedsStatusBarAppearanceUpdate text in status bar will not appear on iPhone X, for other devices it's not needed.

    0 讨论(0)
  • 2020-12-13 07:20

    I encountered exactly the same issue and used the same code as @entropid did. However the accepted solution did not work for me.

    It took me hours to come up with the following one-line fix that made things working for me:

    - (void)viewWillLayoutSubviews {
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    }
    
    0 讨论(0)
提交回复
热议问题