iOS設置某個界面強制橫屏,進入就橫屏

原文地址:http://www.cnblogs.com/niit-soft-518/p/5611298.html 

方案一:

使用 presentViewController

1.首先設置項目 支持的屏幕方向


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 //支持旋轉
14 -(BOOL)shouldAutorotate{
15     return [self.topViewController shouldAutorotate];
16 }
17 
18 //支持的方向
19 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
20     return [self.topViewController supportedInterfaceOrientations];
21 }
22 
23 @end
複製代碼

在AppDelegate中設置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中,重寫旋轉方法 和 支持的方向

複製代碼
1 //支持旋轉
2 -(BOOL)shouldAutorotate{
3     return YES;
4 }
5 
6 //支持的方向 因爲界面A我們只需要支持豎屏
7 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
8     return UIInterfaceOrientationMaskPortrait;
9 }
複製代碼

4.界面A跳轉界面B的方法:

1 -(void)pushaction{
2     ViewControllertwo *vc = [[ViewControllertwo alloc]init];
3     //使用 presentViewController 跳轉
4     [self presentViewController:vc animated:YES completion:nil];
5 }

5.界面B重寫 旋轉方法 和 支持的方向

複製代碼
 1 //支持旋轉
 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到別的界面就不太好實現了,所以,我又找了半天,又找到了解決方案。

1.設置項目支持的旋轉方向:

 

2.創建子類CusNavViewController 繼承UINavigationController

3.界面A設置支持的方向 和 是否可以旋轉

複製代碼
 1 //是否可以旋轉
 2 - (BOOL)shouldAutorotate
 3 {
 4     return false;
 5 }
 6 //支持的方向
 7 -(UIInterfaceOrientationMask)supportedInterfaceOrientations
 8 {
 9     return UIInterfaceOrientationMaskPortrait;
10 }
複製代碼

4.push進去的界面B 設置 方向 和 旋轉

複製代碼
 1 //支持的方向
 2 -(UIInterfaceOrientationMask)supportedInterfaceOrientations
 3 {
 4     return UIInterfaceOrientationMaskLandscapeLeft;
 5 }
 6 
 7 //是否可以旋轉
 8 -(BOOL)shouldAutorotate
 9 {
10     return YES;
11 }
複製代碼

5.界面B設置物理設備方向:

複製代碼
//setOrientation 在3.0以後變爲私有方法了,不能直接去調用此方法,否則後果就是被打回。
在網上搜了很多很久,都是這種調用私有方法的:
//強制橫屏,會被打回。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}

不能直接調用,但是可以間接的去調用,下面的方法就是利用 KVO機制去間接調用,多次驗證不會被打回,放心!
-(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的方法 間接的調用此方法,可以上架,不會被打回。

至於這裏爲什麼要 多寫這兩行代碼:

NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

請參考博客:http://www.jianshu.com/p/6c45fa2bb970

 

 

 

方法三:

*iOS中可以直接調用某個對象的消息方式有兩種

*1.performSelector:withObject;

 *2.NSInvocation

複製代碼
 1 //使用這裏的代碼也是oK的。 這裏利用 NSInvocation 調用 對象的消息
 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 }
複製代碼

第一個參數需要接收一個指針,也就是傳遞值的時候需要傳遞地址

第二個參數:需要給指定方法的第幾個參數傳值

注意:設置參數的索引時不能從0開始,因爲0已經被self(target)佔用,1已經被_cmd(selector)佔用在NSInvocation的官方文檔中已經說明

(_cmd在Objective-C的方法中表示當前方法的selector,正如同self表示當前方法調用的對象實例。)

[invocationsetArgument:&valatIndex:2];

調用NSInvocation對象的invoke方法*只要調用invocation的invoke方法,就代表需要執行NSInvocation對象中制定對象的指定方法,並且傳遞指定的參數

[invocationinvoke];

 

 
 
方法一GitHub地址:
https://github.com/zhuxinleibandou/-Demo
 
方法二 和 方法三 的GitHub地址:
https://github.com/zhuxinleibandou/PushHPDemo

發佈了26 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章