適配iOS16橫屏操作
if (@available(iOS 16.0, *)) {
@try {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *ws = (UIWindowScene *)array[0];
Class GeometryPreferences = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");
id geometryPreferences = [[GeometryPreferences alloc]init];
[geometryPreferences setValue:@(UIInterfaceOrientationMaskLandscapeRight) forKey:@"interfaceOrientations"];
SEL sel_method = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");
void (^ErrorBlock)(NSError *err) = ^(NSError *err){
NSLog(@"調(diào)用了Block%@",err);
};
if ([ws respondsToSelector:sel_method]) {
(((void (*)(id, SEL,id,id))[ws methodForSelector:sel_method])(ws, sel_method,geometryPreferences,ErrorBlock));
}
} @catch (NSException *exception) {
NSLog(@"*****************************華麗分割線**************************");
NSLog(@"%@",exception);
} @finally {
}
} else {
// 強制旋轉(zhuǎn)回來
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 = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
2022.09.02更新
在調(diào)用上面的方法前執(zhí)行以下方法
/** 控制旋轉(zhuǎn) */
- (void)configSupportedInterfaceOrientations:(int)number
{
id appDel = [[UIApplication sharedApplication] delegate];
SEL sel_clientstate = NSSelectorFromString(@"setClientstate:");
if ([appDel respondsToSelector:sel_clientstate]) {
(((void (*)(id, SEL, NSUInteger))[appDel methodForSelector:sel_clientstate])(appDel, sel_clientstate, number));
}
}
其中clientstate這個是一個狀態(tài)值,用來控制 supportedInterfaceOrientationsFor
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
switch clientstate {
case 1:
return .allButUpsideDown
case 2:
return .landscape
default:
return .portrait
}
}