2012-11-06 113 views
2

我想限制一個視圖控制器在UINavigationController之上。要做到這一點我已經創建了一個UINavigationController子類,並實現2種方法我想限制一些視圖控制器在ios6橫向

- (BOOL)shouldAutorotate { 
return [[self.viewControllers lastObject] shouldAutorotate];} 

- (NSUInteger)supportedInterfaceOrientations { 
return [[self.viewControllers lastObject] supportedInterfaceOrientations];} 

我要上的UINavigationController(這是根視圖控制器)的頂部的第一個視圖 - 控制應在肖像模式和下一個視圖控制器,我」從根視圖控制器推出的應該是風景模式(僅限)。

所以我重寫這兩個視圖控制器中的這兩種方法。 在根視圖控制器

- (BOOL)shouldAutorotate { 
return NO;} 

- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskPortrait;} 

在下一視圖控制器

- (BOOL)shouldAutorotate { 
return YES;} 

- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskLandscape;} 

其工作良好,但不能完全。我第一次將視圖控制器按肖像模式顯示(不限制爲如我所期望的那樣),並且一旦我旋轉設備/模擬器並且其工作正常並且僅限於橫向模式。

任何人都可以幫忙嗎?

回答

-2

UIViewController具有以下功能。您可以在視圖控制器中執行此操作,以在該位置控制縱向方向。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     // Return YES for supported orientations 

    if(interfaceOrientation == UIInterfaceOrientationPortrait) 
     return YES; 
    else 
     return NO; 

    } 
+0

感謝您的快速回復suresh,但在ios6中,該方法不會調用。這就是爲什麼我們使用其他兩種方法。 – MohanVydehi

+0

對不起,我沒有注意到。 –

1

試試看。

打電話給viewWillAppear中的這一個會明確告訴設備跳轉到縱向。

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait]; 

我不認爲這是正確的解決方案。但是如果你沒有別的選擇,你可以使用它。 快樂編碼:)

+0

無法正常工作........ –

+0

感謝哥們,我花了一天多的時間來實現這一目標。 –

0

üpresentnewcontroller

SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
[self.navigationController presentViewController:objSecondViewController animated:NO completion:^{}]; 

newcontroller

- (BOOL)shouldAutorotate { 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskLandscape; 
} 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeLeft; 
} 
-1

這爲我工作。剛剛嘗試使用此:

1)YourApp-Info.plist

  • 再添加一個陣列支持的接口方向
  • 添加您需要的方向,以這本詞典

參見下面的截圖:

enter image description here

2)Project Target

  • 選擇從支持的界面取向

所需取向參見下面的截圖:

enter image description here

+0

這將如何限制特定的控制器? – Abhishek

相關問題