美文网首页
UIWebView播放视频强制横屏

UIWebView播放视频强制横屏

作者: 开米肉丝酱 | 来源:发表于2017-12-12 14:59 被阅读0次

    iOS11 很好的支持了webView播放视频的横屏问题 下面是iOS11之前版本的处理

    首先在AppDegelate中添加一个控制全屏的开关

    @property (nonatomic,assign)BOOL isFull;
    

    在 WebView所在的Controller 注册2个观察通知

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
    

    实现 通知方法

    - (void)begainFullScreen:(NSNotification *)notification {// 开始播放
        
        [[UIApplication sharedApplication]setStatusBarHidden:YES];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.isFull = YES;
    }
    
    
    - (void)endFullScreen:(NSNotification *)notification {// 开始播放
      
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.isFull = NO;
        }
    }
    

    现在只要进入webView进入全屏就可以开启横屏模式
    但是从全屏返回并没进入竖屏所以我们需要进行强制竖屏归正

    //强制归正:
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val =UIInterfaceOrientationPortrait;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
    

    最后添加显示状态栏

     [[UIApplication sharedApplication]setStatusBarHidden:NO];
    

    相关文章

      网友评论

          本文标题:UIWebView播放视频强制横屏

          本文链接:https://www.haomeiwen.com/subject/sdcnixtx.html