Autorotating video played from HTML5 <video> in UIWebView

后端 未结 1 1845
感动是毒
感动是毒 2021-01-03 05:31

My app, for some reasons, only supports portrait orientation.
However, in some cases I need to display videos from a UIWebView (with video tag) and it would

相关标签:
1条回答
  • 2021-01-03 06:09

    I've have done something very similar. You have to modify the UIView stack to force the app to call shouldAutorotateToInterfaceOrientation when you pop the controller.

    In your WebViewController set to allow the autorotate:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
    }
    

    In the parent controller disallow autorotate:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    

    Create and assign a UINavigationControllerDelegate.
    In the delegate, temporarily modify the UIView stack when the controller pops or pushes:

    - (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // Force portrait by changing the view stack to force autorotation!
    if ([UIDevice currentDevice].orientation != UIInterfaceOrientationPortrait) {
        if (![viewController isKindOfClass:[MovieWebViewController class]]) {
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            UIView *view = [window.subviews objectAtIndex:0];
            [view removeFromSuperview];
            [window insertSubview:view atIndex:0];
        }
    }
    

    It's kind of a dirty hack, but it works.

    0 讨论(0)
提交回复
热议问题