代碼控制屏幕橫豎旋轉(zhuǎn)

屏幕旋轉(zhuǎn)控制分動態(tài)旋轉(zhuǎn)和手動代碼控制旋轉(zhuǎn)乔遮,多見于視頻播放。

一 .APP設(shè)置

  1. 在配置中關(guān)閉屏幕方向。


    info.plist
  2. AppDelegate添加如下方法 返回所需要的旋轉(zhuǎn)方向坎匿。代碼設(shè)置的方向優(yōu)先級將大于info.plist中的設(shè)置。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
  1. UITabBarController方向由選中的controller的方向控制,在UITabBarController中添加如下設(shè)置:
- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return   [self.selectedViewController preferredInterfaceOrientationForPresentation];;
}
  1. UINavigationController中由topViewController方向決定
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

二. 旋轉(zhuǎn)控制

  1. 在需要動態(tài)控制旋轉(zhuǎn)的controller配置:
//是否支持自動轉(zhuǎn)屏
- (BOOL)shouldAutorotate
{
    return YES;
}
// 支持哪些屏幕方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
// 默認(rèn)的屏幕方向(當(dāng)前ViewController必須是通過模態(tài)出來的UIViewController(模態(tài)帶導(dǎo)航的無效)方式展現(xiàn)出來的雷激,才會調(diào)用這個方法)
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return   UIInterfaceOrientationPortrait;
}
  1. 手動控制旋轉(zhuǎn)
/// 旋轉(zhuǎn)
/// @param aUIDeviceOrientation 需要設(shè)置的旋轉(zhuǎn)方向
-(void)changeWithDeviceOrientation:(UIDeviceOrientation)aUIDeviceOrientation{
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    if (aUIDeviceOrientation != deviceOrientation) {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            [[UIDevice currentDevice] setValue:@(aUIDeviceOrientation) forKey:@"orientation"];
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(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
  • 正文 為了忘掉前任,我火速辦了婚禮玖详,結(jié)果婚禮上把介,老公的妹妹穿的比我還像新娘。我一直安慰自己蟋座,他們只是感情好拗踢,可當(dāng)我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著向臀,像睡著了一般巢墅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上券膀,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天君纫,我揣著相機與錄音,去河邊找鬼芹彬。 笑死蓄髓,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的雀监。 我是一名探鬼主播双吆,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼会前!你這毒婦竟也來了好乐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瓦宜,失蹤者是張志新(化名)和其女友劉穎蔚万,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體临庇,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡反璃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了假夺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淮蜈。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖已卷,靈堂內(nèi)的尸體忽然破棺而出梧田,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布裁眯,位于F島的核電站鹉梨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏穿稳。R本人自食惡果不足惜存皂,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望逢艘。 院中可真熱鬧旦袋,春花似錦、人聲如沸它改。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽搔课。三九已至,卻和暖如春截亦,著一層夾襖步出監(jiān)牢的瞬間爬泥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工崩瓤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留袍啡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓却桶,卻偏偏與公主長得像境输,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子颖系,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,060評論 2 355