美文网首页
iOS设置横竖屏

iOS设置横竖屏

作者: 末日小妖女 | 来源:发表于2023-08-02 15:45 被阅读0次

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];

        }

    }

相关文章

网友评论

      本文标题:iOS设置横竖屏

      本文链接:https://www.haomeiwen.com/subject/rbrppdtx.html