iPad横竖屏适配

我现在用到的横竖屏比较简单,等以后遇到复杂的再来更新吧

最简单的横竖屏

1、勾选可旋转方向


根据需求,自己勾选旋转方向

此时,便可以随便旋转了,只是我们还需要更新布局
2、在我们需要更新布局的view中,重写方法

 // 更新界面布局
override func layoutSubviews() {
    [super layoutSubviews];
     //通过状态栏电池图标判断横竖屏
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
        //竖屏布局
    } else {
        //横屏布局
    }
}

3、控制器中如果要更新布局,需调用方法

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        // 屏幕旋转调用
        if (size.width > size.height) {
            //横屏设置
        }else{
            //竖屏设置
        }
    }

这里,最简单的横竖屏设置完成

基础知识

三种枚举:UIDeviceOrientation、UIInterfaceOrientation、UIInterfaceOrientationMask

1、设备方向:UIDeviceOrientation

UIDeviceOrientation是指设备iPhone或者iPad本身旋转的方向,设备方向包括7中,其中有一种是未知方向,旋转方向是根据home键来判断的,源码如下

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

监听当前设备旋转的方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChange)
                     name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 - (BOOL)onDeviceOrientationDidChange{
    //获取当前设备Device
    UIDevice *device = [UIDevice currentDevice] ;
    //识别当前设备的旋转方向
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕向上平躺");
            break;

        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕向下平躺");
            break;

        case UIDeviceOrientationUnknown:
            //系统当前无法识别设备朝向,可能是倾斜
            NSLog(@"未知方向");
            break;

        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左橫置");
            break;

        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;

        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;

        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;

        default:
            NSLog(@"無法识别");
            break;
    }
    return YES;
}

注意:设备方向智能获取,不能更改

2、界面方向:UIInterfaceOrientation

界面方向可以自己设置

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
3、界面方向:UIInterfaceOrientationMask
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

我觉得,这个就是多了两种枚举,让我们在使用的时候更方便一些,别的,我也没看出来啥‍♀️
当我们在某一个界面,需要单独设置旋转方向的时候,就使用以下方法

override var shouldAutorotate: Bool {
        // 支持界面自动旋转,返回true,否则返回false
        return true
    }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // 支持的旋转方向
       return UIInterfaceOrientationMask.all
    }
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        // 进入界面设置默认显示方向
       return UIInterfaceOrientation.portrait
    }

你可能感兴趣的:(iPad横竖屏适配)