ios支持屏幕自动旋转,需要分两种情况考虑:锁定和不锁定
锁定情况下,屏幕不会随着手机的转动而旋转屏幕
不锁定情况下,屏幕会根据代码中设置的支持方向自动旋转
设置支持屏幕方向的位置:TARGETS-General-Deployment Info info.plist Appdelegate
如果需要旋转,需要设置
- (BOOL)shouldAutorotate {
return YES;
}
Appdelegate代码
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
横屏代码:
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.isLand?UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
if(@available(iOS16.0, *)) {
[self setNeedsUpdateOfSupportedInterfaceOrientations];
[self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene*scene = (UIWindowScene*)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
[scenerequestGeometryUpdateWithPreferences:geometryPreferenceserrorHandler:^(NSError*_Nonnullerror) {
NSLog(@"wuwuFQ:%@", error);
}];
}else{
if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {
SELselector =NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocationsetSelector:selector];
[invocationsetTarget:[UIDevicecurrentDevice]];
int val = self.isLand?UIInterfaceOrientationLandscapeLeft:UIInterfaceOrientationPortrait;
[invocationsetArgument:&valatIndex:2];
[invocationinvoke];
}
}