iOS 強制轉屏 強制橫屏 強制豎屏

今天項目中遇到正在看視頻的時候賬號被擠,如果當時是橫屏的情況下,需要強制豎屏。真頭疼,網上找了好多方法,終于解決啦。O(∩_∩)O~

強制橫屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

強制豎屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

強制轉屏

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    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 = orientation;
        // 從2開始是因為0 1 兩個參數已經被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容