ios开发中如何控制屏幕旋转

2021-06-01  本文已影响0人  songjk

1.设置app支持的屏幕方向

在info.plist中指定支持的方向或者在appDelegate中设置
// 如何在info.plist中设置app支持的方向
info中设置方向.png
// 如何在appDelegate中设置app支持的方向
// 如果不重写该方法 会默认使用info.plist中的设置
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (all) {
            return UIInterfaceOrientationMaskAll;
        }
        return UIInterfaceOrientationMaskPortrait;
        
    }

2.单独设置控制器支持的方向

// 当设备(不是app)的方向改变时,
// 系统会调用根控制器(比如nav)或者最顶层modal出的控制器的该方法
// 如果控制器的该方法支持新的方向,并且app也支持新的方向,系统会旋转window和控制器
// 系统调用该方法的前提是 shouldAutorotate 方法返回true
// 如果你的app支持多任务,系统不会掉用该方法,因为你的app必须支持所有方向
// 如果不重写该方法,ipad默认返回UIInterfaceOrientationMaskAll,iphone默认返回UIInterfaceOrientationMaskAllButUpsideDown
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    // 是否允许旋转,如果不重写默认返回true
    - (BOOL)shouldAutorotate
    {
        for (UIViewController *vc in self.childViewControllers) {
            if ([vc isKindOfClass:[你想旋转的控制器 class]]) {
                return YES;
            }
        }
        return NO;
    }

3.在代码中旋转设备方向

 [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];

4.控制器旋转到设备目前的方向

[UIViewController attemptRotationToDeviceOrientation];
上一篇 下一篇

猜你喜欢

热点阅读