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

原文鏈接https://my.oschina.net/huqiji/blog/3031940第一種方法會(huì)出現(xiàn)無(wú)法轉(zhuǎn)屏的問題,我就沒有列出來,大家想看的話就去原文里自己去試吧,我這邊用的是第二種,親測(cè)有效,廢話不多說直接上代碼。

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

//強(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];

}

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

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

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

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

}

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

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

}

第五步:

push控制器:

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

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)容合作請(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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