iPad橫豎屏適配

我現(xiàn)在用到的橫豎屏比較簡(jiǎn)單,等以后遇到復(fù)雜的再來(lái)更新吧

最簡(jiǎn)單的橫豎屏

1、勾選可旋轉(zhuǎn)方向


根據(jù)需求,自己勾選旋轉(zhuǎn)方向

此時(shí),便可以隨便旋轉(zhuǎn)了,只是我們還需要更新布局
2、在我們需要更新布局的view中,重寫(xiě)方法

 // 更新界面布局
override func layoutSubviews() {
    [super layoutSubviews];
     //通過(guò)狀態(tài)欄電池圖標(biāo)判斷橫豎屏
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
        //豎屏布局
    } else {
        //橫屏布局
    }
}

3、控制器中如果要更新布局,需調(diào)用方法

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        // 屏幕旋轉(zhuǎn)調(diào)用
        if (size.width > size.height) {
            //橫屏設(shè)置
        }else{
            //豎屏設(shè)置
        }
    }

這里,最簡(jiǎn)單的橫豎屏設(shè)置完成

基礎(chǔ)知識(shí)

三種枚舉:UIDeviceOrientation、UIInterfaceOrientation、UIInterfaceOrientationMask

1、設(shè)備方向:UIDeviceOrientation

UIDeviceOrientation是指設(shè)備iPhone或者iPad本身旋轉(zhuǎn)的方向,設(shè)備方向包括7中,其中有一種是未知方向,旋轉(zhuǎn)方向是根據(jù)home鍵來(lái)判斷的,源碼如下

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;

監(jiān)聽(tīng)當(dāng)前設(shè)備旋轉(zhuǎn)的方法:

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

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 - (BOOL)onDeviceOrientationDidChange{
    //獲取當(dāng)前設(shè)備Device
    UIDevice *device = [UIDevice currentDevice] ;
    //識(shí)別當(dāng)前設(shè)備的旋轉(zhuǎn)方向
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕向上平躺");
            break;

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

        case UIDeviceOrientationUnknown:
            //系統(tǒng)當(dāng)前無(wú)法識(shí)別設(shè)備朝向,可能是傾斜
            NSLog(@"未知方向");
            break;

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

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

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

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

        default:
            NSLog(@"無(wú)法識(shí)別");
            break;
    }
    return YES;
}

注意:設(shè)備方向智能獲取,不能更改

2、界面方向:UIInterfaceOrientation

界面方向可以自己設(shè)置

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;

我覺(jué)得,這個(gè)就是多了兩種枚舉,讓我們?cè)谑褂玫臅r(shí)候更方便一些,別的,我也沒(méi)看出來(lái)啥???♀?
當(dāng)我們?cè)谀骋粋€(gè)界面,需要單獨(dú)設(shè)置旋轉(zhuǎn)方向的時(shí)候,就使用以下方法

override var shouldAutorotate: Bool {
        // 支持界面自動(dòng)旋轉(zhuǎn),返回true,否則返回false
        return true
    }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // 支持的旋轉(zhuǎn)方向
       return UIInterfaceOrientationMask.all
    }
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        // 進(jìn)入界面設(shè)置默認(rèn)顯示方向
       return UIInterfaceOrientation.portrait
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容