此文写于2022年08月03日,距离iOS16正式版推出还有一个多月的时间,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;

UIWindowSceneGeometryPreferencesIOS 有个实例方法传入一个枚举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 {

}

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: