最近有一個項目,例如:A界面跳轉(zhuǎn)到B界面,A界面是豎屏的,B界面進入就要橫屏。
花了半天的時間在網(wǎng)上搜索解決方案,有些論壇的大牛也就貼兩行代碼,具體實現(xiàn)也沒有,對我們這種菜鳥造成一萬點真實傷害。為了避免后人在浪費時間,在這里我整理一下,并且上傳Demo到GitHub。在iOS7 8 9 上運行都OK.
在這里我整理了3種解決方案。
原文地址:http://www.cnblogs.com/niit-soft-518/p/5611298.html
方案一:
使用 presentViewController
1.首先設(shè)置項目 支持的屏幕方向
2.寫一個子類CusNavigationController 繼承 UINavigationController,在CusNavigationController中重寫方法:shouldAutorotate 和 supportedInterfaceOrientations
1 @implementation CusNavViewController 2 3 - (void)viewDidLoad { 4 [super viewDidLoad]; 5 // Do any additional setup after loading the view. 6 } 7 8 - (void)didReceiveMemoryWarning { 9 [super didReceiveMemoryWarning];10 // Dispose of any resources that can be recreated.11 }12 13 //支持旋轉(zhuǎn)14 -(BOOL)shouldAutorotate{15 return [self.topViewController shouldAutorotate];16 }17 18 //支持的方向19 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {20 return [self.topViewController supportedInterfaceOrientations];21 }22 23 @end
在AppDelegate中設(shè)置RootViewController
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 4 [self.window makeKeyAndVisible]; 5 ViewController *vc =[[ViewController alloc]init]; 6 CusNavViewController *nav = [[CusNavViewController alloc]initWithRootViewController:vc]; 7 [self.window setRootViewController:nav]; 8 return YES; 9 10 }
3.最重要的來咯,界面A中,重寫旋轉(zhuǎn)方法 和 支持的方向
1 //支持旋轉(zhuǎn)2 -(BOOL)shouldAutorotate{3 return YES;4 }5 6 //支持的方向 因為界面A我們只需要支持豎屏7 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {8 return UIInterfaceOrientationMaskPortrait;9 }
4.界面A跳轉(zhuǎn)界面B的方法:
1 -(void)pushaction{2 ViewControllertwo *vc = [[ViewControllertwo alloc]init];3 //使用 presentViewController 跳轉(zhuǎn)4 [self presentViewController:vc animated:YES completion:nil];5 }
5.界面B重寫 旋轉(zhuǎn)方法 和 支持的方向
1 //支持旋轉(zhuǎn) 2 -(BOOL)shouldAutorotate{ 3 return YES; 4 } 5 // 6 //支持的方向 7 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 8 return UIInterfaceOrientationMaskLandscapeLeft; 9 }10 11 //一開始的方向 很重要12 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{13 return UIInterfaceOrientationLandscapeLeft;14 }
GitHub Demo地址:https://github.com/zhuxinleibandou/-Demo
原文地址:http://www.cnblogs.com/niit-soft-518/p/5611298.html
方案二:
使用方案一presentViewController確實很不錯,但是畢竟也有些不方便,如果想用在界面使用Nav push到別的界面就不太好實現(xiàn)了,所以,我又找了半天,又找到了解決方案。
1.設(shè)置項目支持的旋轉(zhuǎn)方向:
2.創(chuàng)建子類CusNavViewController 繼承UINavigationController
3.界面A設(shè)置支持的方向 和 是否可以旋轉(zhuǎn)
1 //是否可以旋轉(zhuǎn) 2 - (BOOL)shouldAutorotate 3 { 4 return false; 5 } 6 //支持的方向 7 -(UIInterfaceOrientationMask)supportedInterfaceOrientations 8 { 9 return UIInterfaceOrientationMaskPortrait;10 }
4.push進去的界面B 設(shè)置 方向 和 旋轉(zhuǎn)
1 //支持的方向 2 -(UIInterfaceOrientationMask)supportedInterfaceOrientations 3 { 4 return UIInterfaceOrientationMaskLandscapeLeft; 5 } 6 7 //是否可以旋轉(zhuǎn) 8 -(BOOL)shouldAutorotate 9 {10 return YES;11 }
5.界面B設(shè)置物理設(shè)備方向:
//setOrientation 在3.0以后變?yōu)樗接蟹椒?,不能直接去調(diào)用此方法,否則后果就是被打回。
在網(wǎng)上搜了很多很久,都是這種調(diào)用私有方法的:
//強制橫屏,會被打回。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
不能直接調(diào)用,但是可以間接的去調(diào)用,下面的方法就是利用 KVO機制去間接調(diào)用,多次驗證不會被打回,放心!
-(void)viewWillAppear:(BOOL)animated{ NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];}
這里不是直接使用蘋果的私有變量,而是利用kvo的方法 間接的調(diào)用此方法,可以上架,不會被打回。
至于這里為什么要 多寫這兩行代碼:
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
請參考博客:http://www.jianshu.com/p/6c45fa2bb970
方法三:
*iOS中可以直接調(diào)用某個對象的消息方式有兩種
*1.performSelector:withObject;
*2.NSInvocation
1 //使用這里的代碼也是oK的。 這里利用 NSInvocation 調(diào)用 對象的消息 2 - (void) viewWillAppear:(BOOL)animated 3 { 4 [super viewWillAppear:animated]; 5 if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) { 6 7 SEL selector = NSSelectorFromString(@"setOrientation:"); 8 9 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];10 11 [invocation setSelector:selector];12 13 [invocation setTarget:[UIDevice currentDevice]];14 15 int val = UIInterfaceOrientationLandscapeLeft;//橫屏16 17 [invocation setArgument:&val atIndex:2];18 19 [invocation invoke];20 21 }22 }
第一個參數(shù)需要接收一個指針,也就是傳遞值的時候需要傳遞地址
第二個參數(shù):需要給指定方法的第幾個參數(shù)傳值
注意:設(shè)置參數(shù)的索引時不能從0開始,因為0已經(jīng)被self(target)占用,1已經(jīng)被_cmd(selector)占用在NSInvocation的官方文檔中已經(jīng)說明
(_cmd在Objective-C的方法中表示當前方法的selector,正如同self表示當前方法調(diào)用的對象實例。)
[invocationsetArgument:&valatIndex:2];
調(diào)用NSInvocation對象的invoke方法*只要調(diào)用invocation的invoke方法,就代表需要執(zhí)行NSInvocation對象中制定對象的指定方法,并且傳遞指定的參數(shù)
[invocationinvoke];
關(guān)于NSInvocation的博客
http://blog.csdn.net/onlyou930/article/details/7449102
http://www.jianshu.com/p/da96980648b6
http://my.oschina.net/u/2340880/blog/398552?fromerr=sAJ1ndvB
原文地址:http://www.cnblogs.com/niit-soft-518/p/5611298.html