橫屏處理OC&Swift4.1

首先我們要先設(shè)置app允許的窗口朝向

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;

如果你沒(méi)有寫這個(gè)方法伺帘,那會(huì)從plist里獲取默認(rèn)值。
該方法來(lái)自UIApplicationDelegate新锈,所以要寫在AppDelegate里。
所使用的枚舉
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait,//home鍵朝下
UIInterfaceOrientationMaskLandscapeLeft,//home鍵朝左
UIInterfaceOrientationMaskLandscapeRight ,//home鍵朝右
UIInterfaceOrientationMaskPortraitUpsideDown,//home鍵朝上
UIInterfaceOrientationMaskLandscape,//home鍵朝左或者朝右
UIInterfaceOrientationMaskAll,//上下左右都可以
UIInterfaceOrientationMaskAllButUpsideDown,//home鍵朝左朝右朝下
}

swift寫法

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeLeft);
//可以豎屏和home鍵在左邊橫屏
 }

OC寫法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

然后設(shè)置頁(yè)面是否允許翻轉(zhuǎn)和頁(yè)面允許的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//默認(rèn)值iPad UIInterfaceOrientationMaskAll iPhone  UIInterfaceOrientationMaskAllButUpsideDown
- (BOOL)shouldAutorotate //默認(rèn)值為YES
- (BOOL)prefersStatusBarHidden //默認(rèn)值為YES

supportedInterfaceOrientations這個(gè)方法表示該view controller允許的朝向,但是這個(gè)方向必須是application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window中或者plist中允許的方向亥至。
橫屏的時(shí)候狀態(tài)欄會(huì)默認(rèn)隱藏。所以我們需要設(shè)置prefersStatusBarHidden
OC

- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}  

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

Swift

    override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeRight.union(UIInterfaceOrientationMask.portraitUpsideDown))
    }
    
    override var prefersStatusBarHidden: Bool {
        return false
    }

這三個(gè)方法都是UIViewController下贱迟。也就是說(shuō)UIViewcontroller及他的子類都可以用這三個(gè)方法(UITabBarController, UINavigationController也是UIViewcontroller的子類)姐扮。
如果rootViewController為UITabBarController實(shí)例的時(shí)候。
只有UITabBarController的shouldAutorotate起作用衣吠。UITabBarController下的UINavigationController和UIViewcontroller的shouldAutorotate都有不能控制窗口翻轉(zhuǎn)茶敏,但是UIViewcontroller的shouldAutorotate為YES時(shí)可以造成狀態(tài)欄的翻轉(zhuǎn),狀態(tài)欄的翻轉(zhuǎn)方向受UIViewcontroller的supportedInterfaceOrientations影響缚俏。
效果如下:


屏幕快照 2018-03-05 上午11.22.09.png

如果rootViewController為UINavigationController實(shí)例的時(shí)候惊搏。UIViewcontroller的shouldAutorotate為YES時(shí)可以造成狀態(tài)欄的翻轉(zhuǎn)但是不會(huì)對(duì)窗口的翻轉(zhuǎn)有影響贮乳。狀態(tài)欄的翻轉(zhuǎn)方向受UIViewcontroller的supportedInterfaceOrientations影響。
由UIViewcontroller present出來(lái)的頁(yè)面不受rootViewController的影響恬惯。翻轉(zhuǎn)情況由他自己的shouldAutorotate和supportedInterfaceOrientations控制向拆。

強(qiáng)制翻轉(zhuǎn):
當(dāng)退出可翻轉(zhuǎn)頁(yè)面的時(shí)候我們往往會(huì)用到強(qiáng)制翻轉(zhuǎn)。原理就是一個(gè)KVC酪耳。
OC

//先判斷Orientation狀態(tài)
if ((long)self.preferredInterfaceOrientationForPresentation != 1) {
        NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    }

Swift

//注意在swift中系統(tǒng)不能將enum直接轉(zhuǎn)成int型浓恳,所以需要手動(dòng)get rawValue
if self.preferredInterfaceOrientationForPresentation != .portrait {
        
         UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.unknown.rawValue), forKey: "orientation")
                    UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.portrait.rawValue), forKey: "orientation")
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市碗暗,隨后出現(xiàn)的幾起案子颈将,更是在濱河造成了極大的恐慌,老刑警劉巖言疗,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晴圾,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡噪奄,警方通過(guò)查閱死者的電腦和手機(jī)死姚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)梗醇,“玉大人知允,你說(shuō)我怎么就攤上這事⌒鸾鳎” “怎么了温鸽?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)手负。 經(jīng)常有香客問(wèn)我涤垫,道長(zhǎng),這世上最難降的妖魔是什么竟终? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任蝠猬,我火速辦了婚禮,結(jié)果婚禮上统捶,老公的妹妹穿的比我還像新娘榆芦。我一直安慰自己,他們只是感情好喘鸟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布匆绣。 她就那樣靜靜地躺著,像睡著了一般什黑。 火紅的嫁衣襯著肌膚如雪崎淳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天愕把,我揣著相機(jī)與錄音拣凹,去河邊找鬼森爽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嚣镜,可吹牛的內(nèi)容都是我干的爬迟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼祈惶,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼雕旨!你這毒婦竟也來(lái)了扮匠?” 一聲冷哼從身側(cè)響起捧请,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棒搜,沒(méi)想到半個(gè)月后疹蛉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡力麸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年可款,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片克蚂。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡闺鲸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出埃叭,到底是詐尸還是另有隱情摸恍,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布赤屋,位于F島的核電站立镶,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏类早。R本人自食惡果不足惜媚媒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涩僻。 院中可真熱鬧缭召,春花似錦、人聲如沸逆日。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)屏富。三九已至晴竞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狠半,已是汗流浹背噩死。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工颤难, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人已维。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓行嗤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親垛耳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子栅屏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容