最近有一個項目,例如:A界面跳轉到B界面,A界面是豎屏的,B界面進入就要橫屏。
花了半天的時間在網上搜索解決方案,有些論壇的大牛也就貼兩行代碼,具體實現也沒有,對我們這種菜鳥造成一萬點真實傷害。為了避免后人在浪費時間,在這里我整理一下,並且上傳Demo到GitHub。在iOS7 8 9 上運行都OK.
在這里我整理了3種解決方案。
方案一:
使用 presentViewController
1.首先設置項目 支持的屏幕方向

2.寫一個子類CusNavigationController 繼承 UINavigationController,在CusNavigationController中重寫方法:shouldAutorotate 和 supportedInterfaceOrientations
@implementation CusNavViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //支持旋轉 -(BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate]; } //支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } @end
在AppDelegate中設置RootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; [self.window makeKeyAndVisible]; ViewController *vc =[[ViewController alloc]init]; CusNavViewController *nav = [[CusNavViewController alloc]initWithRootViewController:vc]; [self.window setRootViewController:nav]; return YES; }
3.最重要的來咯,界面A中,重寫旋轉方法 和 支持的方向
//支持旋轉 -(BOOL)shouldAutorotate{ return YES; } //支持的方向 因為界面A我們只需要支持豎屏 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
4.界面A跳轉界面B的方法:
-(void)pushaction{ ViewControllertwo *vc = [[ViewControllertwo alloc]init]; //使用 presentViewController 跳轉 [self presentViewController:vc animated:YES completion:nil]; }
5.界面B重寫 旋轉方法 和 支持的方向
//支持旋轉 -(BOOL)shouldAutorotate{ return YES; } // //支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft; } //一開始的方向 很重要 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationLandscapeLeft; }
方案二:
使用方案一presentViewController確實很不錯,但是畢竟也有些不方便,如果想用在界面使用Nav push到別的界面就不太好實現了,所以,我又找了半天,又找到了解決方案。
1.設置項目支持的旋轉方向:

2.創建子類CusNavViewController 繼承UINavigationController
3.界面A設置支持的方向 和 是否可以旋轉
//是否可以旋轉 - (BOOL)shouldAutorotate { return false; } //支持的方向 -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
4.push進去的界面B 設置 方向 和 旋轉
//支持的方向 -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft; } //是否可以旋轉 -(BOOL)shouldAutorotate { return YES; }
5.界面B設置物理設備方向:
//setOrientation 在3.0以后變為私有方法了,不能直接去調用此方法,否則后果就是被打回。 在網上搜了很多很久,都是這種調用私有方法的: //強制橫屏,會被打回。 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationLandscapeRight]; } 不能直接調用,但是可以間接的去調用,下面的方法就是利用 KVO機制去間接調用,多次驗證不會被打回,放心! - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillAppear:animated]; NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"]; }
備注:使用這種方法可以不設置 2 3 4步 我沒設置也成功了
這里不是直接使用蘋果的私有變量,而是利用kvo的方法 間接的調用此方法,可以上架,不會被打回。
至於這里為什么要 多寫這兩行代碼:
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
方法三:
iOS中可以直接調用某個對象的消息方式有兩種
1.performSelector:withObject;
2.NSInvocation
//使用這里的代碼也是oK的。 這里利用 NSInvocation 調用 對象的消息 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; 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 = UIInterfaceOrientationLandscapeLeft;//橫屏 [invocation setArgument:&val atIndex:2]; [invocation invoke]; } }
第一個參數需要接收一個指針,也就是傳遞值的時候需要傳遞地址
第二個參數:需要給指定方法的第幾個參數傳值
注意:設置參數的索引時不能從0開始,因為0已經被self(target)占用,1已經被_cmd(selector)占用在NSInvocation的官方文檔中已經說明
(_cmd在Objective-C的方法中表示當前方法的selector,正如同self表示當前方法調用的對象實例。)
[invocationsetArgument:&valatIndex:2];
調用NSInvocation對象的invoke方法*只要調用invocation的invoke方法,就代表需要執行NSInvocation對象中制定對象的指定方法,並且傳遞指定的參數
[invocationinvoke];
關於NSInvocation的博客
