iOS全局禁止橫屏,特定頁(yè)面允許橫屏的解決方法

最近在做一個(gè)視頻實(shí)時(shí)監(jiān)控的app,用到螢石開放平臺(tái)的SDK來(lái)進(jìn)行開發(fā)。螢石的demo里面在特定頁(yè)面通過簡(jiǎn)單的幾句代碼調(diào)用就實(shí)現(xiàn)了全屏和退出全屏的功能。

//進(jìn)入全屏
- (IBAction)large:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
//退出全屏
- (IBAction)largeBack:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

而我在按照螢石官方文檔一步一步將SDK導(dǎo)入我的工程,卻出現(xiàn)無(wú)法全屏的問題。

在參考了iOS 屏幕方向那點(diǎn)事兒、UIWebView 全屏播放視頻解決辦法幾篇博文之后,最后采用如下方法解決了全屏問題:

1. 全局禁止橫屏

在appdelegate.h添加以下屬性:

/***  是否允許橫屏的標(biāo)記 */
@property (nonatomic,assign)BOOL allowRotation;

appdelegate.m添加如下代碼:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {//如果設(shè)置了allowRotation屬性,支持全屏
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;//默認(rèn)全局不支持橫屏
}

2. 在需要支持橫屏的界面改變allowRotation屬性

//進(jìn)入全屏
-(void)begainFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    
    //強(qiáng)制歸正:
    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];
    }
}

viewWillAppearviewWillDisappear分別調(diào)用以上方法,在該控制器下即可順利支持全屏。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容