iOS16.0:画面回転

この記事は 2022 年 8 月 3 日に書かれたものですiOS16。正式版のリリースまではまだ 1 か月以上あります。iOS16 betaこのバージョンには API の多くの変更が加えられています。今日は をベースに、画面の回転について説明しますXcode 14.0 beta4
前回の画面回転ではエラーが報告されます。
[Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)

UIWindowScene.requestGeometryUpdate(_:)それでは、使い方を見てみましょう?

ここに画像の説明を挿入します

- (void)requestGeometryUpdateWithPreferences:(UIWindowSceneGeometryPreferences *)geometryPreferences 
                                errorHandler:(void (^)(NSError *error))errorHandler;

Methodパラメータを入力する必要があることを確認してくださいUIWindowSceneGeometryPreferences *

ここに画像の説明を挿入します
UIWindowSceneGeometryPreferencesこれは新しい API でもあり、明らかにUIWindowSceneGeometryPreferencesIOS必要なものです。

ここに画像の説明を挿入します

- (instancetype)initWithInterfaceOrientations:(UIInterfaceOrientationMask)interfaceOrientations;

UIWindowSceneGeometryPreferencesIOSenumeration を渡すインスタンス メソッドがありUIInterfaceOrientationMask、この時点で API の使用状況を理解できます。

iOS16.0以降の横画面コード:

        if (@available(iOS 16.0, *)) {
    
    
            [self setNeedsUpdateOfSupportedInterfaceOrientations];
            [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];

            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *scene = (UIWindowScene *)array[0];
            UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
            [scene requestGeometryUpdateWithPreferences:geometryPreferences
                errorHandler:^(NSError * _Nonnull error) {
    
    
                NSLog(@"wuwuFQ:%@", error);
            }];
            
        } else {
    
    

		}

おすすめ

転載: blog.csdn.net/wujakf/article/details/126133680