iPad橫豎屏適配

我現(xiàn)在用到的橫豎屏比較簡單闲昭,等以后遇到復雜的再來更新吧

最簡單的橫豎屏

1栖榨、勾選可旋轉方向


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

此時坟奥,便可以隨便旋轉了树瞭,只是我們還需要更新布局
2、在我們需要更新布局的view中爱谁,重寫方法

 // 更新界面布局
override func layoutSubviews() {
    [super layoutSubviews];
     //通過狀態(tài)欄電池圖標判斷橫豎屏
    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中雨效,其中有一種是未知方向,旋轉方向是根據(jù)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;

監(jiān)聽當前設備旋轉的方法:

[[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:
            //系統(tǒng)當前無法識別設備朝向,可能是傾斜
            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
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末躯肌,一起剝皮案震驚了整個濱河市者春,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌清女,老刑警劉巖钱烟,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異嫡丙,居然都是意外死亡拴袭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門曙博,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拥刻,“玉大人,你說我怎么就攤上這事羊瘩√┘眩” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵尘吗,是天一觀的道長逝她。 經(jīng)常有香客問我,道長睬捶,這世上最難降的妖魔是什么黔宛? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮擒贸,結果婚禮上臀晃,老公的妹妹穿的比我還像新娘觉渴。我一直安慰自己,他們只是感情好徽惋,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布案淋。 她就那樣靜靜地躺著,像睡著了一般险绘。 火紅的嫁衣襯著肌膚如雪踢京。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天宦棺,我揣著相機與錄音瓣距,去河邊找鬼。 笑死代咸,一個胖子當著我的面吹牛蹈丸,可吹牛的內容都是我干的。 我是一名探鬼主播呐芥,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼逻杖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贩耐?” 一聲冷哼從身側響起弧腥,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎潮太,沒想到半個月后管搪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡铡买,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年更鲁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奇钞。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡澡为,死狀恐怖,靈堂內的尸體忽然破棺而出景埃,到底是詐尸還是另有隱情媒至,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布谷徙,位于F島的核電站拒啰,受9級特大地震影響,放射性物質發(fā)生泄漏完慧。R本人自食惡果不足惜谋旦,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧册着,春花似錦拴孤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至司顿,卻和暖如春绽媒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背免猾。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留囤热,地道東北人猎提。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像旁蔼,于是被迫代替她去往敵國和親锨苏。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355