自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

代碼處理iOS的橫豎屏旋轉(zhuǎn)

移動開發(fā) iOS
在處理iOS橫豎屏?xí)r,經(jīng)常會和UIDeviceOrientation、UIInterfaceOrientation和UIInterfaceOrientationMask這三個(gè)枚舉類型打交道,它們從不同角度描述了屏幕旋轉(zhuǎn)方向。

一、監(jiān)聽屏幕旋轉(zhuǎn)方向

在處理iOS橫豎屏?xí)r,經(jīng)常會和UIDeviceOrientation、UIInterfaceOrientation和UIInterfaceOrientationMask這三個(gè)枚舉類型打交道,它們從不同角度描述了屏幕旋轉(zhuǎn)方向。

1、UIDeviceOrientation:設(shè)備方向

iOS的設(shè)備方向是通過iOS的加速計(jì)來獲取的。

1)iOS定義了以下七種設(shè)備方向

  1. typedef NS_ENUM(NSInteger, UIDeviceOrientation) { 
  2.  
  3.     UIDeviceOrientationUnknown,                 // 未知方向,可能是設(shè)備(屏幕)斜置 
  4.  
  5.     UIDeviceOrientationPortrait,                // 設(shè)備(屏幕)直立 
  6.  
  7.     UIDeviceOrientationPortraitUpsideDown,      // 設(shè)備(屏幕)直立,上下顛倒 
  8.  
  9.     UIDeviceOrientationLandscapeLeft,           // 設(shè)備(屏幕)向左橫置 
  10.  
  11.     UIDeviceOrientationLandscapeRight,          // 設(shè)備(屏幕)向右橫置 
  12.  
  13.     UIDeviceOrientationFaceUp,                  // 設(shè)備(屏幕)朝上平躺 
  14.  
  15.     UIDeviceOrientationFaceDown                 // 設(shè)備(屏幕)朝下平躺 
  16.  
  17. };  

說明:UIDeviceOrientation參考home鍵方向,如:home方向在右,設(shè)備(屏幕)方向向左(UIDeviceOrientationLandscapeLeft)

2)讀取設(shè)備方向

UIDevice單例代表當(dāng)前的設(shè)備。從這個(gè)單例中可以獲得的信息設(shè)備,如設(shè)備方向orientation。

  1. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 

3)監(jiān)聽、處理和移除 設(shè)備方向改變的通知

當(dāng)設(shè)備方向變化時(shí)候,發(fā)出UIDeviceOrientationDidChangeNotification通知;注冊監(jiān)聽該通知,可以針對不同的設(shè)備方向處理視圖展示。

  1. //開啟和監(jiān)聽 設(shè)備旋轉(zhuǎn)的通知(不開啟的話,設(shè)備方向一直是UIInterfaceOrientationUnknown) 
  2.  
  3. if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) { 
  4.  
  5.     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
  6.  
  7.  
  8. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:) 
  9.  
  10.                                      name:UIDeviceOrientationDidChangeNotification object:nil]; 
  11.  
  12.   
  13.  
  14.   
  15.  
  16. //設(shè)備方向改變的處理 
  17.  
  18. - (void)handleDeviceOrientationChange:(NSNotification *)notification{ 
  19.  
  20.   
  21.  
  22.     UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
  23.  
  24.     switch (ddeviceOrientation) { 
  25.  
  26.         case UIDeviceOrientationFaceUp: 
  27.  
  28.             NSLog(@"屏幕朝上平躺"); 
  29.  
  30.             break; 
  31.  
  32.   
  33.  
  34.         case UIDeviceOrientationFaceDown: 
  35.  
  36.             NSLog(@"屏幕朝下平躺"); 
  37.  
  38.             break; 
  39.  
  40.   
  41.  
  42.         case UIDeviceOrientationUnknown: 
  43.  
  44.             NSLog(@"未知方向"); 
  45.  
  46.             break; 
  47.  
  48.   
  49.  
  50.         case UIDeviceOrientationLandscapeLeft: 
  51.  
  52.             NSLog(@"屏幕向左橫置"); 
  53.  
  54.             break; 
  55.  
  56.   
  57.  
  58.         case UIDeviceOrientationLandscapeRight: 
  59.  
  60.             NSLog(@"屏幕向右橫置"); 
  61.  
  62.             break; 
  63.  
  64.   
  65.  
  66.         case UIDeviceOrientationPortrait: 
  67.  
  68.             NSLog(@"屏幕直立"); 
  69.  
  70.             break; 
  71.  
  72.   
  73.  
  74.         case UIDeviceOrientationPortraitUpsideDown: 
  75.  
  76.             NSLog(@"屏幕直立,上下顛倒"); 
  77.  
  78.             break; 
  79.  
  80.   
  81.  
  82.         default
  83.  
  84.             NSLog(@"無法辨識"); 
  85.  
  86.             break; 
  87.  
  88.     } 
  89.  
  90.  
  91.   
  92.  
  93. //最后在dealloc中移除通知 和結(jié)束設(shè)備旋轉(zhuǎn)的通知 
  94.  
  95. - (void)dealloc{ 
  96.  
  97.     //... 
  98.  
  99.     [[NSNotificationCenter defaultCenter]removeObserver:self]; 
  100.  
  101.     [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications]; 

說明:手機(jī)鎖定豎屏后,UIDeviceOrientationDidChangeNotification通知就失效了。

2、UIInterfaceOrientation:界面方向

界面方向是反應(yīng)iOS中界面的方向,它和Home按鈕的方向是一致的。

1)iOS定義了以下五種界面方向

  1. typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { 
  2.  
  3.     UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,       //未知方向 
  4.  
  5.     UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,               //界面直立 
  6.  
  7.     UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  //界面直立,上下顛倒 
  8.  
  9.     UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,   //界面朝左 
  10.  
  11.     UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft    //界面朝右 
  12.  
  13. } __TVOS_PROHIBITED;  

說明:從定義可知,界面方向和設(shè)別方向有對應(yīng)關(guān)系,如界面的豎直方向就是 設(shè)備的豎直方向:UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown

2)讀取界面方向

UIInterfaceOrientation和狀態(tài)欄有關(guān),通過UIApplication的單例調(diào)用statusBarOrientation來獲取

  1. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

3)監(jiān)聽、處理和移除 界面方向改變的通知

當(dāng)界面方向變化時(shí)候,先后發(fā)出UIApplicationWillChangeStatusBarOrientationNotification和UIApplicationDidChangeStatusBarOrientationNotification通知;注冊監(jiān)聽這兩個(gè)通知,可以針對不同的界面方向處理視圖展示。

  1. //以監(jiān)聽UIApplicationDidChangeStatusBarOrientationNotification通知為例 
  2.  
  3. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleStatusBarOrientationChange:) 
  4.  
  5.                                      name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
  6.  
  7.   
  8.  
  9.   
  10.  
  11. //界面方向改變的處理 
  12.  
  13. - (void)handleStatusBarOrientationChange: (NSNotification *)notification{ 
  14.  
  15.   
  16.  
  17.     UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
  18.  
  19.     switch (interfaceOrientation) { 
  20.  
  21.   
  22.  
  23.         case UIInterfaceOrientationUnknown: 
  24.  
  25.             NSLog(@"未知方向"); 
  26.  
  27.             break; 
  28.  
  29.   
  30.  
  31.         case UIInterfaceOrientationPortrait: 
  32.  
  33.             NSLog(@"界面直立"); 
  34.  
  35.             break; 
  36.  
  37.   
  38.  
  39.         case UIInterfaceOrientationPortraitUpsideDown: 
  40.  
  41.             NSLog(@"界面直立,上下顛倒"); 
  42.  
  43.             break; 
  44.  
  45.   
  46.  
  47.         case UIInterfaceOrientationLandscapeLeft: 
  48.  
  49.             NSLog(@"界面朝左"); 
  50.  
  51.             break; 
  52.  
  53.   
  54.  
  55.         case UIInterfaceOrientationLandscapeRight: 
  56.  
  57.             NSLog(@"界面朝右"); 
  58.  
  59.             break; 
  60.  
  61.   
  62.  
  63.         default
  64.  
  65.             break; 
  66.  
  67.     } 
  68.  
  69.  
  70.   
  71.  
  72. //最后在dealloc中移除通知 
  73.  
  74. - (void)dealloc{ 
  75.  
  76.     //... 
  77.  
  78.     [[NSNotificationCenter defaultCenter]removeObserver:self]; 
  79.  
  80.     [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications]; 
  81.  
  82.  

說明:手機(jī)鎖定豎屏后,UIApplicationWillChangeStatusBarOrientationNotification和UIApplicationDidChangeStatusBarOrientationNotification通知也失效了。

3、UIInterfaceOrientationMask

UIInterfaceOrientationMask是為了集成多種UIInterfaceOrientation而定義的類型,和ViewController相關(guān),一共有7種

1)iOS中的UIInterfaceOrientationMask定義

  1. typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { 
  2.  
  3.     UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), 
  4.  
  5.     UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), 
  6.  
  7.     UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), 
  8.  
  9.     UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), 
  10.  
  11.     UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  12.  
  13.     UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), 
  14.  
  15.     UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  16.  
  17. } __TVOS_PROHIBITED; 

2)UIInterfaceOrientationMask的使用

在ViewController可以重寫- (UIInterfaceOrientationMask)supportedInterfaceOrientations方法返回類型,來決定UIViewController可以支持哪些界面方向。

  1. //支持界面直立 
  2.  
  3. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  4.  
  5.     return UIInterfaceOrientationMaskPortrait; 
  6.  
  7.  

總結(jié):UIDeviceOrientation(設(shè)備方向)和UIInterfaceOrientation(屏幕方向)是兩個(gè)不同的概念。前者代表了設(shè)備的一種狀態(tài),而后者是屏幕為了應(yīng)對不同的設(shè)備狀態(tài),做出的用戶界面上的響應(yīng)。在iOS設(shè)備旋轉(zhuǎn)時(shí),由UIKit接收到旋轉(zhuǎn)事件,然后通過AppDelegate通知當(dāng)前程序的UIWindow對象,UIWindow對象通知它的rootViewController,如果該rootViewController支持旋轉(zhuǎn)后的屏幕方向,完成旋轉(zhuǎn),否則不旋轉(zhuǎn);彈出的ViewController也是如此處理。

二、視圖控制器中旋轉(zhuǎn)方向的設(shè)置

0、關(guān)于禁止橫屏的操作(不建議)

比較常規(guī)的方法有兩種。

方法1:在項(xiàng)目的General–>Deployment Info–>Device Orientation中,只勾選Portrait(豎屏)

 

勾選Portrait.png

方法2:Device Orientation默認(rèn)設(shè)置,在Appdelegate中實(shí)現(xiàn)supportedInterfaceOrientationsForWindow:只返回UIInterfaceOrientationMaskPortraitt(豎屏)

  1. -  (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {   
  2.  
  3.      return UIInterfaceOrientationMaskPortrait;   
  4.  
  5.  

說明:極少的APP中所有界面都是豎屏的,因?yàn)榭倳薪缑嫘枰С謾M屏,如視頻播放頁。所以不建議設(shè)置禁止APP頁面橫屏。

下面介紹如何讓項(xiàng)目中的 視圖控制器中旋轉(zhuǎn)方向的設(shè)置

1、APP支持多個(gè)方向

 

APP支持多個(gè)方向.png

說明:如此,APP支持橫屏和豎屏了,但是具體視圖控制器支持的頁面方向還需要進(jìn)一步處理。由于不支持豎屏顛倒(Upside Down),即使設(shè)備上下顛倒,通過API也不會獲得設(shè)備、屏幕上下顛倒方向的。

2、支持ViewController屏幕方向設(shè)置

1)關(guān)鍵函數(shù)

視圖控制器支持的界面方向主要由以下三個(gè)函數(shù)控制

  1. //是否自動旋轉(zhuǎn),返回YES可以自動旋轉(zhuǎn),返回NO禁止旋轉(zhuǎn)   
  2.  
  3. - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;   
  4.  
  5.   
  6.  
  7. //返回支持的方向   
  8.  
  9. - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;   
  10.  
  11.   
  12.  
  13. //由模態(tài)推出的視圖控制器 優(yōu)先支持的屏幕方向 
  14.  
  15. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  

2) QSBaseViewController設(shè)置

  1. //QSBaseViewController.h 
  2.  
  3. @interface QSBaseController : UIViewController 
  4.  
  5.   
  6.  
  7. @end 
  8.  
  9.   
  10.  
  11. //QSBaseViewController.m 
  12.  
  13. @implementation QSBaseController 
  14.  
  15.   
  16.  
  17.   //#pragma mark - 控制屏幕旋轉(zhuǎn)方法 
  18.  
  19. //是否自動旋轉(zhuǎn),返回YES可以自動旋轉(zhuǎn),返回NO禁止旋轉(zhuǎn) 
  20.  
  21. - (BOOL)shouldAutorotate{ 
  22.  
  23.     return NO
  24.  
  25.  
  26.   
  27.  
  28. //返回支持的方向 
  29.  
  30. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  31.  
  32.     return UIInterfaceOrientationMaskPortrait; 
  33.  
  34.  
  35.   
  36.  
  37. //由模態(tài)推出的視圖控制器 優(yōu)先支持的屏幕方向 
  38.  
  39. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
  40.  
  41.     return UIInterfaceOrientationPortrait; 
  42.  
  43.  
  44. @end  

說明1:QSBaseViewController默認(rèn)不支持旋轉(zhuǎn),只支持 界面豎直方向,項(xiàng)目中的Controller都繼承自QSBaseViewController,可以通過重寫這三個(gè)方法來讓Controller支持除豎屏之外的方向或旋轉(zhuǎn)。

3) 在QSNavigationController設(shè)置

目標(biāo):通過QSNavigationController來push視圖控制器時(shí),把支持屏幕旋轉(zhuǎn)的設(shè)置交給最新push進(jìn)來([self.viewControllers lastObject])的viewController來設(shè)置。

  1. //QSNavigationController.h 
  2.  
  3. @interface QSNavigationController : UINavigationController 
  4.  
  5.   
  6.  
  7. @end 
  8.  
  9.   
  10.  
  11. //QSNavigationController.m 
  12.  
  13. @implementation QSNavigationController 
  14.  
  15.   
  16.  
  17. #pragma mark - 控制屏幕旋轉(zhuǎn)方法 
  18.  
  19. - (BOOL)shouldAutorotate{   
  20.  
  21.     return [[self.viewControllers lastObject]shouldAutorotate]; 
  22.  
  23.  
  24.   
  25.  
  26. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  27.  
  28.     return [[self.viewControllers lastObject]supportedInterfaceOrientations]; 
  29.  
  30.  
  31.   
  32.  
  33. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
  34.  
  35.     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
  36.  
  37.  
  38. @end  

4) 在QSTabBarController設(shè)置

目標(biāo):TabBarController通常作為整個(gè)程序的rootViewController,UITabBar上面顯示的每一個(gè)Tab都對應(yīng)著一個(gè)ViewController;每點(diǎn)擊一個(gè)Tab,出現(xiàn)的ViewController(self.selectedViewController)對屏幕旋轉(zhuǎn)和支持方向的設(shè)置 交給其自身去控制。

  1. //QSTabBarController.h 
  2.  
  3. @interface QSTabBarController : UITabBarController 
  4.  
  5.   
  6.  
  7. @end 
  8.  
  9.   
  10.  
  11. //QSTabBarController.m 
  12.  
  13. @implementation QSTabBarController 
  14.  
  15.   
  16.  
  17. #pragma mark - 控制屏幕旋轉(zhuǎn)方法 
  18.  
  19. - (BOOL)shouldAutorotate{ 
  20.  
  21.     return [self.selectedViewController shouldAutorotate]; 
  22.  
  23.  
  24.   
  25.  
  26. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  27.  
  28.     return [self.selectedViewController supportedInterfaceOrientations]; 
  29.  
  30.  
  31.   
  32.  
  33. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
  34.  
  35.     return [self.selectedViewController preferredInterfaceOrientationForPresentation]; 
  36.  
  37.  
  38. @end  

三、屏幕旋轉(zhuǎn)方向下的視圖處理

1、屏幕旋轉(zhuǎn)時(shí),建議監(jiān)聽UIApplicationDidChangeStatusBarOrientationNotification

原因1:supportedInterfaceOrientations方法中最終返回的是 多個(gè)界面方向。

原因2(最重要的原因):我們真正要處理的是頁面方向發(fā)生旋轉(zhuǎn)UI的變化。而在設(shè)備的物理方向發(fā)生旋轉(zhuǎn)的時(shí)候,如果此時(shí)當(dāng)前控制器的頁面并沒有旋轉(zhuǎn),我們這時(shí)改變UI布局,可能就發(fā)生問題了。

2、屏幕的寬高處理

1)在iOS 8之后,當(dāng)屏幕旋轉(zhuǎn)的時(shí)候,[[UIScreen mainScreen] bounds]也發(fā)生了改變。如橫屏?xí)r候的屏幕寬度 其實(shí)是豎屏的時(shí)候屏幕的高度。

2)我們處理視圖布局時(shí)候,如果使用到屏幕的寬高,不要直接使用SCREEN_HEIGHT和SCREEN_WIDTH,而使用SCREEN_MIN和SCREEN_MAX

  1. #define SCREEN_HEIGHT CGRectGetHeight([[UIScreen mainScreen] bounds]) 
  2.  
  3. #define SCREEN_WIDTH  CGRectGetWidth([[UIScreen mainScreen] bounds]) 
  4.  
  5.   
  6.  
  7. #define SCREEN_MIN MIN(SCREEN_HEIGHT,SCREEN_WIDTH) 
  8.  
  9. #define SCREEN_MAX MAX(SCREEN_HEIGHT,SCREEN_WIDTH)  

說明:豎屏?xí)r候,寬是SCREEN_MIN,高是SCREEN_MAX;橫屏?xí)r候,寬是SCREEN_MAX,高是SCREEN_MIN。

3、屏幕旋轉(zhuǎn)下處理Demo

  1. //監(jiān)聽UIApplicationDidChangeStatusBarOrientationNotification的處理 
  2.  
  3. - (void)handleStatusBarOrientationChange: (NSNotification *)notification{ 
  4.  
  5.   
  6.  
  7.     UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
  8.  
  9.     BOOL isLandscape = NO
  10.  
  11.     switch (interfaceOrientation) { 
  12.  
  13.   
  14.  
  15.         case UIInterfaceOrientationUnknown: 
  16.  
  17.             NSLog(@"未知方向"); 
  18.  
  19.             break; 
  20.  
  21.   
  22.  
  23.         case UIInterfaceOrientationPortrait: 
  24.  
  25.         case UIInterfaceOrientationPortraitUpsideDown: 
  26.  
  27.             isLandscape = NO
  28.  
  29.             break; 
  30.  
  31.   
  32.  
  33.         case UIInterfaceOrientationLandscapeLeft: 
  34.  
  35.         case UIInterfaceOrientationLandscapeRight: 
  36.  
  37.             isLandscape = YES; 
  38.  
  39.             break; 
  40.  
  41.   
  42.  
  43.         default
  44.  
  45.             break; 
  46.  
  47.     } 
  48.  
  49.     if (isLandscape) { 
  50.  
  51.         self.tableView.frame = CGRectMake(0, 0, SCREEN_MAX, SCREEN_MIN - 44); 
  52.  
  53.     }else
  54.  
  55.         self.tableView.frame = CGRectMake(0, 0, SCREEN_MIN, SCREEN_MAX - 64); 
  56.  
  57.     } 
  58.  
  59.   
  60.  
  61.     [self.tableView reloadData]; 
  62.  
  63.  

說明:當(dāng)然也可以選擇使用Masonry這樣優(yōu)秀的AutoLayout布局第三方庫來處理,storyBoard來布局次之。

4、屏幕旋轉(zhuǎn)下處理Demo效果圖

 

豎屏下效果.png

 

橫屏下效果.png

5、屏幕旋轉(zhuǎn)處理的建議

1)旋轉(zhuǎn)前后,view當(dāng)前顯示的位置盡量不變

2)旋轉(zhuǎn)過程中,暫時(shí)界面操作的響應(yīng)

3)視圖中有tableview的話,旋轉(zhuǎn)后,強(qiáng)制 [tableview reloadData],保證在方向變化以后,新的row能夠充滿全屏。

四、強(qiáng)制橫屏

APP中某些頁面,如視頻播放頁,一出現(xiàn)就要求橫屏。這些橫屏頁面或模態(tài)彈出、或push進(jìn)來。

1、模態(tài)彈出ViewController情況下 強(qiáng)制橫屏的設(shè)置

  1. //QSShow3Controller.m 
  2.  
  3. - (BOOL)shouldAutorotate{ 
  4.  
  5.     return NO
  6.  
  7.  
  8.   
  9.  
  10. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  11.  
  12.     return UIInterfaceOrientationMaskLandscapeRight; 
  13.  
  14.  
  15.   
  16.  
  17. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
  18.  
  19.     return UIInterfaceOrientationLandscapeRight; 
  20.  
  21.  
  22.   
  23.  
  24. //模態(tài)彈出 
  25.  
  26. QSShow3Controller *vc = [[QSShow3Controller alloc]init]; 
  27.  
  28. [self presentViewController:vc animated:YES completion:nil];  

說明:這種情況比較簡單處理。

2、push推入ViewController情況下 強(qiáng)制橫屏的設(shè)置

  1. //QSShow4Controller.m 
  2.  
  3. -(void)viewWillAppear:(BOOL)animated{ 
  4.  
  5.   
  6.  
  7.    [super viewWillAppear:animated]; 
  8.  
  9.    [self setInterfaceOrientation:UIInterfaceOrientationLandscapeRight]; 
  10.  
  11.  
  12.   
  13.  
  14. //強(qiáng)制轉(zhuǎn)屏(這個(gè)方法最好放在BaseVController中) 
  15.  
  16. - (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation{ 
  17.  
  18.   
  19.  
  20.     if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { 
  21.  
  22.         SEL selector  = NSSelectorFromString(@"setOrientation:"); 
  23.  
  24.         NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; 
  25.  
  26.         [invocation setSelector:selector]; 
  27.  
  28.         [invocation setTarget:[UIDevice currentDevice]]; 
  29.  
  30.         // 從2開始是因?yàn)榍皟蓚€(gè)參數(shù)已經(jīng)被selector和target占用 
  31.  
  32.         [invocation setArgument:&orientation atIndex:2]; 
  33.  
  34.         [invocation invoke]; 
  35.  
  36.     } 
  37.  
  38.  
  39.   
  40.  
  41. //必須返回YES 
  42.  
  43. - (BOOL)shouldAutorotate{ 
  44.  
  45.     return YES; 
  46.  
  47.  
  48.   
  49.  
  50. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
  51.  
  52.     return UIInterfaceOrientationMaskLandscapeRight; 
  53.  
  54.  
  55.   
  56.  
  57. //Push推入 
  58.  
  59. QSShow4Controller *vc = [[QSShow4Controller alloc]init]; 
  60.  
  61. [self.navigationController pushViewController:vc animated:YES];  

說明:蘋果不允許直接調(diào)用setOrientation方法,否則有被拒的風(fēng)險(xiǎn);使用NSInvocation對象給[UIDevice currentDevice]發(fā)消息,強(qiáng)制改變設(shè)備方向,使其頁面方向?qū)?yīng)改變,這是蘋果允許的。

五、其他

1、 APP啟動時(shí),手機(jī)橫屏下,首頁UI(該頁面只支持豎屏)出錯(cuò)(add by 2017/6/20)

  1. //設(shè)置設(shè)置狀態(tài)欄豎屏 
  2.  
  3.   [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];  

以上詳細(xì)源碼參考:QSRotationScreenDemo

https://github.com/buaa0300/QSKitDemo/tree/master/QSRotationScreenDemo 

責(zé)任編輯:龐桂玉 來源: iOS大全
相關(guān)推薦

2013-08-21 11:15:54

iOS橫豎屏方案

2013-05-23 10:51:28

Android開發(fā)移動開發(fā)橫豎屏切換

2012-05-22 14:26:15

XNA 橫豎屏設(shè)置

2016-09-18 10:51:01

JavascriptHtml5移動應(yīng)用

2011-07-29 10:21:03

iPad 橫豎屏 切換

2017-11-20 11:07:23

Selection代碼邏輯分支

2010-01-25 15:23:12

Android橫豎屏切

2023-11-02 09:42:21

iOS屏幕旋轉(zhuǎn)

2011-06-08 15:05:43

J2ME

2013-04-09 10:03:29

iOS6.0旋轉(zhuǎn)兼容

2012-12-24 08:54:47

iOSUnity3D

2012-01-10 09:32:12

iOS 5.1設(shè)備四核處理器

2013-07-29 05:04:19

Cocos2dx橫屏豎

2017-12-20 16:58:49

iOS離屏渲染屏幕渲染

2022-01-25 10:11:10

紅綠屏蘋果BUG

2013-07-29 04:24:40

iOS開發(fā)學(xué)習(xí)ViewControl

2017-11-16 15:21:06

代碼taste方法

2012-02-02 16:35:42

Silverlight低級別觸屏處理

2018-05-16 07:41:29

圖片代碼資源

2011-07-25 16:31:51

iOS XML 文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號