iOS强制横屏或强制竖屏

原文链接https://my.oschina.net/huqiji/blog/3031940第一种方法会出现无法转屏的问题,我就没有列出来,大家想看的话就去原文里自己去试吧,我这边用的是第二种,亲测有效,废话不多说直接上代码。

第一种解决方案(不推荐,直接跳过看第二种解决方案):

//强制转屏

- (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开始是因为0 1 两个参数已经被selector和target占用

[invocation setArgument:&val atIndex:2];  

    [invocation invoke]; 

  }}

强制横屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

强制竖屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

只在某一个界面提供转屏的解决方法如下AppDelegate.m下操作

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

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

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

{

//横屏

returnUIInterfaceOrientationMaskLandscapeRight;   

}

//竖屏

returnUIInterfaceOrientationMaskPortrait;

}

/获取界面最上层的控制器

- (UIViewController*)topViewController {

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

}

//一层一层的进行查找判断

- (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; 

  }

}

如果你的应用的根控制器是Nav就把下面这段代码放到Nav根控制器下,如果是TabVC放到TabVC的下面

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

然后在你想横屏的控制器加上这段代码,基本上横屏问题就可以搞定了,前提是你的这个控制器是moda出来的,如果是push的话就要使用上文提到的强制横竖屏的方法,下面这段代码是不起作用的

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskLandscapeRight;

}

第二种解决方案:

灵活设置横竖屏,不用区分Push还是Present,都是可以设置。

第一步:

在AppDelegate.h中添加旋转属性/**

* 是否允许转向

*/@property(nonatomic,assign)BOOLallowRotation;

在AppDelegate.m中添加转屏的代理方法

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

if(self.allowRotation ==YES)

{

//横屏

returnUIInterfaceOrientationMaskLandscape;           

}else{

//竖屏

returnUIInterfaceOrientationMaskPortrait; 

          } 

  }

第二步:

设置横竖屏的核心方法,我是直接把这个方法添加到了UIDevice的分类中,代码如下:

UIDevice+TFDevice.h :

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

* @interfaceOrientation 输入要强制转屏的方向

*/+ (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

第三步:

在需要设置横屏的控制器的ViewDidLoad中添加下面代码:

- (void)viewDidLoad { 

  [superviewDidLoad];   

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;//允许转成横屏

appDelegate.allowRotation =YES;//调用横屏代码[UIDeviceswitchNewOrientation:UIInterfaceOrientationLandscapeRight];

}

第四步 (针对Push出的控制器来说):

需要注意的是push过去的时候变成横屏,pop出去的时候在设置竖屏,此时最好禁用系统的侧滑返回手势。

-(void)viewWillAppear:(BOOL)animated{ 

  [superviewWillAppear:animated];//禁用侧滑手势方法self.navigationController.interactivePopGestureRecognizer.enabled =NO;

}

-(void)viewWillDisappear:(BOOL)animated{   

[superviewWillDisappear:animated];//禁用侧滑手势方法self.navigationController.interactivePopGestureRecognizer.enabled =YES;

}

第五步:

push控制器:

//点击导航栏返回按钮的时候调用,所以Push出的控制器最好禁用侧滑手势:

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//关闭横屏仅允许竖屏//切换到竖屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];  [self.navigationController popViewControllerAnimated:YES];

present控制器:

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//关闭横屏仅允许竖屏//切换到竖屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];    [selfdismissViewControllerAnimated:YEScompletion:nil];

第六步: 上图


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271