iOS16.0: Screen rotation

This article was written on August 3, 2022. It is still iOS16more than a month before the official version is launched. iOS16 betaThe version has many API modifications. Today we are discussing screen rotation, based on Xcode 14.0 beta4.
The previous screen rotation will report an error:
[Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)

So let's see UIWindowScene.requestGeometryUpdate(_:)how to use it?

Insert image description here

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

Find Methodand see that you need to enter parametersUIWindowSceneGeometryPreferences *

Insert image description here
UIWindowSceneGeometryPreferencesIt is also a new API, which is obviously UIWindowSceneGeometryPreferencesIOSwhat we need.

Insert image description here

- (instancetype)initWithInterfaceOrientations:(UIInterfaceOrientationMask)interfaceOrientations;

UIWindowSceneGeometryPreferencesIOSThere is an instance method that passes in an enumeration UIInterfaceOrientationMask. At this point, you can understand the context of API usage.

iOS16.0+ horizontal screen code:

        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 {
    
    

		}

Guess you like

Origin blog.csdn.net/wujakf/article/details/126133680
Recommended