原文連接 https://www.jianshu.com/p/d6cb54d2eaa1
親測第二種我這邊是闊以滴app
//強制轉屏 - (void)interfaceOrientation:(UIInterfaceOrientation)orientation { if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = 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([[self topViewController] class])); if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"想要提供轉屏的控制器的名字"]) { //橫屏 return UIInterfaceOrientationMaskLandscapeRight; } //豎屏 return UIInterfaceOrientationMaskPortrait; } //獲取界面最上層的控制器 - (UIViewController*)topViewController { return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } //一層一層的進行查找判斷 - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController*)rootViewController; return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* nav = (UINavigationController*)rootViewController; return [self topViewControllerWithRootViewController:nav.visibleViewController]; } else if (rootViewController.presentedViewController) { UIViewController* presentedViewController = rootViewController.presentedViewController; return [self topViewControllerWithRootViewController:presentedViewController]; } else { return rootViewController; } }
- (BOOL)shouldAutorotate{ return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; }
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; }
靈活設置橫豎屏,不用區分Push仍是Present,都是能夠設置。atom
在AppDelegate.h中添加旋轉屬性 /** * 是否容許轉向 */ @property(nonatomic,assign)BOOL allowRotation;
在AppDelegate.m中添加轉屏的代理方法 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window { if (self.allowRotation == YES) { //橫屏 return UIInterfaceOrientationMaskLandscape; }else{ //豎屏 return UIInterfaceOrientationMaskPortrait; } }
設置橫豎屏的核心方法,我是直接把這個方法添加到了UIDevice的分類中,代碼以下:代理
#import <UIKit/UIKit.h> @interface UIDevice (TFDevice) /** * @interfaceOrientation 輸入要強制轉屏的方向 */ + (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation; @end
#import "UIDevice+TFDevice.h" @implementation UIDevice (TFDevice) + (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation { NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"]; } @end
在須要設置橫屏的控制器的ViewDidLoad中添加下面代碼:code
- (void)viewDidLoad { [super viewDidLoad]; AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; //容許轉成橫屏 appDelegate.allowRotation = YES; //調用橫屏代碼 [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight]; }
須要注意的是push過去的時候變成橫屏,pop出去的時候在設置豎屏,此時最好禁用系統的側滑返回手勢。blog
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //禁用側滑手勢方法 self.navigationController.interactivePopGestureRecognizer.enabled = NO; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //禁用側滑手勢方法 self.navigationController.interactivePopGestureRecognizer.enabled = YES; }
push控制器:get
//點擊導航欄返回按鈕的時候調用,因此Push出的控制器最好禁用側滑手勢: AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = NO;//關閉橫屏僅容許豎屏 //切換到豎屏 [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait]; [self.navigationController popViewControllerAnimated:YES];
present控制器:it
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = NO;//關閉橫屏僅容許豎屏 //切換到豎屏 [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait]; [self dismissViewControllerAnimated:YES completion:nil];