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
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。