iOS 单页UIWebView播放视频,需要横屏

在项目中遇到了一个小问题,就是在应用本身是竖屏情况下,有某个页面需要支撑横屏时,需要怎么修改。

首先 我需要横屏的 是一个通用UIWebView 播放视频的界面。

第一种方法:

   AppDelegate.h 添加 @property(nonatomic, assign) BOOL allowRotation;

   AppDelegate.m 添加方法

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

        if (self.allowRotation) {

        return UIInterfaceOrientationMaskAll;

        }

        return UIInterfaceOrientationMaskPortrait;

}


再在 需要横屏的页面

viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification obj ect:nil];//进入全屏

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

以及方法:

// 进入全屏

-(void)begainFullScreen

{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    appDelegate.allowRotation = YES;

}

// 退出全屏

-(void)endFullScreen

{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    appDelegate.allowRotation = NO;

    //强制归正:

    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];

    }

}


第二种方法:
创建一个子类,继承UINavigationController
重写三个方法:

- (BOOL)shouldAutorotate

{

    return [self.viewControllers.lastObject shouldAutorotate];

}


- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return [self.viewControllers.lastObject supportedInterfaceOrientations];

}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];

}


再在需要旋转的页面调用一下方法即可:

// 支持设备自动旋转

- (BOOL)shouldAutorotate

{

    return YES;

}


// 支持横竖屏显示

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskAll;

}






     




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值