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

来源:互联网 发布:淘宝暗语 编辑:程序博客网 时间:2024/05/17 23:37

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

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

第一种方法:

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

   AppDelegate.m 添加方法

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

        if (self.allowRotation) {

        returnUIInterfaceOrientationMaskAll;

        }

        returnUIInterfaceOrientationMaskPortrait;

}


再在 需要横屏的页面

viewDidLoad:

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(begainFullScreen)name:UIWindowDidBecomeVisibleNotificationobj ect:nil];//进入全屏

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(endFullScreen)name:UIWindowDidBecomeHiddenNotificationobject:nil];

以及方法:

// 进入全屏

-(void)begainFullScreen

{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];

    appDelegate.allowRotation =YES;

}

// 退出全屏

-(void)endFullScreen

{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];

    appDelegate.allowRotation =NO;

    //强制归正:

    if ([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {

        SEL selector =NSSelectorFromString(@"setOrientation:");

        NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];


        [invocation setSelector:selector];

        [invocation setTarget:[UIDevicecurrentDevice]];

        int val =UIInterfaceOrientationPortrait;

        [invocation setArgument:&valatIndex:2];

        [invocation invoke];

    }

}


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

- (BOOL)shouldAutorotate

{

    return [self.viewControllers.lastObjectshouldAutorotate];

}


- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return [self.viewControllers.lastObjectsupportedInterfaceOrientations];

}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];

}


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

// 支持设备自动旋转

- (BOOL)shouldAutorotate

{

    returnYES;

}


// 支持横竖屏显示

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskAll;

}






     




0 0