2013-12-12 49 views
7

我的應用程序是UITabBarController - > UINavigationController - > UITableViewController - > UIViewController。我如何限制導航控制器層次結構中iOS7中的每個視圖控制器的方向

我想要做的兩件事情:

  1. 旋轉阻止我的tableview,我希望它永遠停留肖像。

  2. FORCE &允許我的UIViewcontroller旋轉landscapeleft。

我所知道的:

  1. 據我所知,在層次結構的頂部視圖控制器控制旋轉。這將是我的UITabBarController?或者更確切地說它是唯一的視圖控制器,它將在objectIndex:0?

  2. 我的項目設置允許肖像,LL和LR旋轉。我認爲這是我需要遵循的模式,以解決這個問題是允許在頂層的ALL旋轉,然後單獨控制每個VC,正確?

這是我迄今爲止在SO中發現的。

因此,對於我的頂級層次結構,我設置項目設置爲允許旋轉到縱向,LL和LR。

,然後在我的tableviewcontroller其中我不想旋轉:

-(BOOL)shouldAutorotate{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

終於在我的UIViewController,我想旋轉:

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

-(BOOL)shouldAutorotate{ 
    return YES; 
} 

然而,這是行不通的。我可以在任何方向旋轉。我也不知道如何強制旋轉LL,當我到達我的uvc時,從我的tablevc中調用模態segue。

任何幫助理解這個混亂將不勝感激:)

回答

6

好吧,在這裏。有點複雜。

項目設置必須允許P, LL & LR

故事板是一個UINavController與推杆按鈕SEGUE到UIViewController一個UITableViewController

故事板中的所有場景都必須推斷爲模擬度量標準中的方向。只是說,經過一段時間後,我測試了很多之後,我把它們放在不同的設置中。

必須有一個類NavController,TableViewController和給定的UIVController。我的應用程序以單視圖開始,然後拖入UITVC,最後將UITVC嵌入到UINVC中。然後我將UIVC連接到通過push segue拖入的UITVC欄按鈕項。

設置應用window.rootvcnavVC,你的頂部層級VC(不要忘記設置ID在故事板或它會崩潰當然):

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; 
    UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"]; 
    self.window.rootViewController = myNavC; 

告訴大老闆UINVC每個人都可以旋轉:

- (BOOL)shouldAutorotate { 
     return self.topViewController.shouldAutorotate;  
    } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
      return self.topViewController.supportedInterfaceOrientations;  
    } 

限制tablevc,所以它不會轉動:

- (BOOL)shouldAutorotate { return NO; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskPortrait); 
    } 

允許最後一個UIVC旋轉:

- (BOOL)shouldAutorotate { return YES; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskAllButUpsideDown);  
    } 
+2

這個工作。但是我發現XCode/IB本身並沒有弄明白這一點,並且「只是工作」而感到沮喪。愚蠢的旋轉廢話。 :/ –

12

簡單但它工作得很好。 IOS 7.1和8

AppDelegate.h

@property() BOOL restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
if(self.restrictRotation) 
    return UIInterfaceOrientationMaskPortrait; 
else 
    return UIInterfaceOrientationMaskAll; 
} 

的ViewController

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
} 

viewDidLoad中

[self restrictRotation:YES]; or NO 
+0

在所有的解決方法中,這隻適用於我..謝謝。 –

+0

最簡單,最容易控制的解決方案 –

+0

如果它在導航控制器中使用,最好在viewWillAppear中使用它...雖然很好的答案.. – geekyaleks

0

性反應

它不應該混淆這裏和UIDeviceOrientation接口取向是我的解決方案

Appdelegate.h

@property() UIInterfaceOrientationMask restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UIInterfaceOrientationMask restrictionOrientation = 0; 

if (_restrictRotation == 0) 
    return UIInterfaceOrientationMaskAll; 

UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation]; 

switch (currentOrientation) 
{ 
    case UIDeviceOrientationPortrait: 

     if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationPortraitUpsideDown: 

     if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeLeft: 

     if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeRight: 

     if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationUnknown: 

     if (!(UIInterfaceOrientationUnknown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    default: 

     NSLog(@"Unknown orientation"); 

     break; 
} 

return restrictionOrientation; 

}

在每個vc添加功能離子

-(void) restrictRotation:(UIInterfaceOrientationMask) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 

}

viewDidAppear

// 
// Set vc orientation 
// 

[self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight]; 
or what you want. 

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"]; 

or what you want. 
相關問題