iOS強(qiáng)制橫屏或強(qiáng)制豎屏

原文鏈接https://my.oschina.net/huqiji/blog/3031940第一種方法會出現(xiàn)無法轉(zhuǎn)屏的問題肥橙,我就沒有列出來镰绎,大家想看的話就去原文里自己去試吧为肮,我這邊用的是第二種初烘,親測有效口注,廢話不多說直接上代碼变擒。

第一種解決方案(不推薦,直接跳過看第二種解決方案):

//強(qiáng)制轉(zhuǎn)屏

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{

if([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {??

? ? SEL selector? =NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];??

?? [invocation setSelector:selector];? ?

? ? [invocation setTarget:[UIDevicecurrentDevice]];intval = orientation;// 從2開始是因?yàn)? 1 兩個(gè)參數(shù)已經(jīng)被selector和target占用

[invocation setArgument:&val atIndex:2];??

? ? [invocation invoke];?

? }}

強(qiáng)制橫屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

強(qiáng)制豎屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

只在某一個(gè)界面提供轉(zhuǎn)屏的解決方法如下AppDelegate.m下操作

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

NSLog(@"0000000---------%@",NSStringFromClass([[selftopViewController]class]));

if([NSStringFromClass([[selftopViewController]class]) isEqualToString:@"想要提供轉(zhuǎn)屏的控制器的名字"])

{

//橫屏

returnUIInterfaceOrientationMaskLandscapeRight;? ?

}

//豎屏

returnUIInterfaceOrientationMaskPortrait;

}

/獲取界面最上層的控制器

- (UIViewController*)topViewController {

return[selftopViewControllerWithRootViewController:[UIApplicationsharedApplication].keyWindow.rootViewController];

}

//一層一層的進(jìn)行查找判斷

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {

if([rootViewController isKindOfClass:[UITabBarControllerclass]]) {UITabBarController* tabBarController = (UITabBarController*)rootViewController;return[selftopViewControllerWithRootViewController:tabBarController.selectedViewController];? ?

}elseif([rootViewController isKindOfClass:[UINavigationControllerclass]]) {

UINavigationController* nav = (UINavigationController*)rootViewController;return[selftopViewControllerWithRootViewController:nav.visibleViewController];? ? }elseif(rootViewController.presentedViewController) {

UIViewController* presentedViewController = rootViewController.presentedViewController;

return[selftopViewControllerWithRootViewController:presentedViewController];?

? }else{

returnrootViewController;?

? }

}

如果你的應(yīng)用的根控制器是Nav就把下面這段代碼放到Nav根控制器下疆导,如果是TabVC放到TabVC的下面

- (BOOL)shouldAutorotate{returnYES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{returnUIInterfaceOrientationMaskPortrait;}

然后在你想橫屏的控制器加上這段代碼赁项,基本上橫屏問題就可以搞定了,前提是你的這個(gè)控制器是moda出來的澈段,如果是push的話就要使用上文提到的強(qiáng)制橫豎屏的方法悠菜,下面這段代碼是不起作用的

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskLandscapeRight;

}

第二種解決方案:

靈活設(shè)置橫豎屏,不用區(qū)分Push還是Present,都是可以設(shè)置败富。

第一步:

在AppDelegate.h中添加旋轉(zhuǎn)屬性/**

* 是否允許轉(zhuǎn)向

*/@property(nonatomic,assign)BOOLallowRotation;

在AppDelegate.m中添加轉(zhuǎn)屏的代理方法

- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(nullableUIWindow*)window{

if(self.allowRotation ==YES)

{

//橫屏

returnUIInterfaceOrientationMaskLandscape;? ? ? ? ? ?

}else{

//豎屏

returnUIInterfaceOrientationMaskPortrait;?

? ? ? ? ? }?

? }

第二步:

設(shè)置橫豎屏的核心方法悔醋,我是直接把這個(gè)方法添加到了UIDevice的分類中,代碼如下:

UIDevice+TFDevice.h :

#import<UIKit/UIKit.h>@interfaceUIDevice(TFDevice)/**

* @interfaceOrientation 輸入要強(qiáng)制轉(zhuǎn)屏的方向

*/+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;@end

UIDevice+TFDevice.m :

#import"UIDevice+TFDevice.h"

@implementationUIDevice(TFDevice)

+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation{

NSNumber*resetOrientationTarget = [NSNumbernumberWithInt:UIInterfaceOrientationUnknown];? ? ? ? ? ? ? ? [[UIDevicecurrentDevice] setValue:resetOrientationTarget forKey:@"orientation"];NSNumber*orientationTarget = [NSNumbernumberWithInt:interfaceOrientation];? ? ? ? ? ?

? ? [[UIDevicecurrentDevice] setValue:orientationTarget forKey:@"orientation"];?

? }

@end

第三步:

在需要設(shè)置橫屏的控制器的ViewDidLoad中添加下面代碼:

- (void)viewDidLoad {?

? [superviewDidLoad];? ?

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;//允許轉(zhuǎn)成橫屏

appDelegate.allowRotation =YES;//調(diào)用橫屏代碼[UIDeviceswitchNewOrientation:UIInterfaceOrientationLandscapeRight];

}

第四步 (針對Push出的控制器來說):

需要注意的是push過去的時(shí)候變成橫屏兽叮,pop出去的時(shí)候在設(shè)置豎屏芬骄,此時(shí)最好禁用系統(tǒng)的側(cè)滑返回手勢猾愿。

-(void)viewWillAppear:(BOOL)animated{?

? [superviewWillAppear:animated];//禁用側(cè)滑手勢方法self.navigationController.interactivePopGestureRecognizer.enabled =NO;

}

-(void)viewWillDisappear:(BOOL)animated{? ?

[superviewWillDisappear:animated];//禁用側(cè)滑手勢方法self.navigationController.interactivePopGestureRecognizer.enabled =YES;

}

第五步:

push控制器:

//點(diǎn)擊導(dǎo)航欄返回按鈕的時(shí)候調(diào)用,所以Push出的控制器最好禁用側(cè)滑手勢:

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//關(guān)閉橫屏僅允許豎屏//切換到豎屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];? [self.navigationController popViewControllerAnimated:YES];

present控制器:

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//關(guān)閉橫屏僅允許豎屏//切換到豎屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];? ? [selfdismissViewControllerAnimated:YEScompletion:nil];

第六步: 上圖


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末账阻,一起剝皮案震驚了整個(gè)濱河市蒂秘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌淘太,老刑警劉巖姻僧,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蒲牧,居然都是意外死亡撇贺,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門冰抢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來松嘶,“玉大人,你說我怎么就攤上這事挎扰〈涠” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵鼓鲁,是天一觀的道長蕴轨。 經(jīng)常有香客問我,道長骇吭,這世上最難降的妖魔是什么橙弱? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮燥狰,結(jié)果婚禮上棘脐,老公的妹妹穿的比我還像新娘。我一直安慰自己龙致,他們只是感情好蛀缝,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著目代,像睡著了一般屈梁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上榛了,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天在讶,我揣著相機(jī)與錄音,去河邊找鬼霜大。 笑死构哺,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的战坤。 我是一名探鬼主播曙强,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼残拐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了碟嘴?” 一聲冷哼從身側(cè)響起溪食,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎娜扇,沒想到半個(gè)月后眠菇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡袱衷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了笑窜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片致燥。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖排截,靈堂內(nèi)的尸體忽然破棺而出嫌蚤,到底是詐尸還是另有隱情,我是刑警寧澤断傲,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布脱吱,位于F島的核電站,受9級特大地震影響认罩,放射性物質(zhì)發(fā)生泄漏箱蝠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一垦垂、第九天 我趴在偏房一處隱蔽的房頂上張望宦搬。 院中可真熱鬧,春花似錦劫拗、人聲如沸间校。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽憔足。三九已至,卻和暖如春酒繁,著一層夾襖步出監(jiān)牢的瞬間滓彰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工欲逃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留找蜜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓稳析,卻偏偏與公主長得像洗做,于是被迫代替她去往敵國和親弓叛。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355