This article was written on August 3, 2022. It is still iOS16
more than a month before the official version is launched. iOS16 beta
The 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?
- (void)requestGeometryUpdateWithPreferences:(UIWindowSceneGeometryPreferences *)geometryPreferences
errorHandler:(void (^)(NSError *error))errorHandler;
Find Method
and see that you need to enter parametersUIWindowSceneGeometryPreferences *
UIWindowSceneGeometryPreferences
It is also a new API, which is obviously UIWindowSceneGeometryPreferencesIOS
what we need.
- (instancetype)initWithInterfaceOrientations:(UIInterfaceOrientationMask)interfaceOrientations;
UIWindowSceneGeometryPreferencesIOS
There 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 {
}