iOS [Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please u...

原代碼是這樣:

      - (void)setInterfaceOrientation:(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;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }

當(dāng)需要強(qiáng)制旋轉(zhuǎn)屏幕時(shí),傳入目標(biāo)的 orientation 達(dá)到強(qiáng)制旋轉(zhuǎn)屏幕的目的。但是,如題所示,在 iOS 16 中,通過(guò)設(shè)置設(shè)置 UIDevice.orientation 來(lái)強(qiáng)制旋轉(zhuǎn)屏幕的方向已不再被支持。
根據(jù)錯(cuò)誤提示,需要使用 UIWindowScene.requestGeometryUpdate(_:) 方法來(lái)實(shí)現(xiàn)。于是修改后的代碼如下:

///強(qiáng)制轉(zhuǎn)換屏幕方向
- (void)ll_interfaceOrientation:(UIInterfaceOrientation)orientation {
    
    if(@available(iOS 16.0, *)) {
        UIScene *windowScene = [[[UIApplication sharedApplication] connectedScenes] anyObject];
        UIWindowSceneGeometryPreferencesIOS *preferences = nil;
        if (orientation == UIInterfaceOrientationLandscapeRight || (orientation == UIInterfaceOrientationLandscapeLeft)) {
            preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
        }else {
            preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
        }
        [(UIWindowScene *)windowScene requestGeometryUpdateWithPreferences:preferences errorHandler:^(NSError * _Nonnull error) {
        }];

    }else {
        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;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 聲明:本文適配以iOS 16 bate 2為基準(zhǔn) 背景 iOS 16在UIKIT上有了一些更改,廢棄掉了一些修改方...
    不正常人類研究中心驢主任閱讀 25,014評(píng)論 32 49
  • 目錄 一、最讓人糾結(jié)的三種枚舉 二、兩種屏幕旋轉(zhuǎn)的觸發(fā)方式 三、屏幕旋轉(zhuǎn)控制的優(yōu)先級(jí) 四、開(kāi)啟屏幕旋轉(zhuǎn)的全局權(quán)限 ...
    來(lái)鬧的閱讀 3,108評(píng)論 0 4
  • iOS屏幕旋轉(zhuǎn)學(xué)習(xí)筆記iOS開(kāi)發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法 1、基本知識(shí)點(diǎn)解讀 了解屏幕旋轉(zhuǎn)首先需要區(qū)分兩種 o...
    Laughingg閱讀 13,792評(píng)論 13 39
  • 1.強(qiáng)制應(yīng)用為橫屏的方法。在view contronller中的實(shí)現(xiàn) (NSUInteger)supportedI...
    the宇亮閱讀 476評(píng)論 0 0
  • 目錄一、最讓人糾結(jié)的三種枚舉二、兩種屏幕旋轉(zhuǎn)的觸發(fā)方式三、屏幕旋轉(zhuǎn)控制的優(yōu)先級(jí)四、開(kāi)啟屏幕旋轉(zhuǎn)的全局權(quán)限五、開(kāi)啟屏...
    Faner_NG閱讀 5,024評(píng)論 2 18

友情鏈接更多精彩內(nèi)容