2012-09-21 20 views
8

我想讓我的幾個應用程序viewcontrollers在iOS 6.0中不旋轉。 這是我做過什麼,使旋轉的iOS 6可能:iOS 6 ViewController正在旋轉,但不應該

1)設置應用程序的窗口RootViewController的中:didFinishLaunchingWithOptions:

self.window.rootViewController = self.tabBarController; 

2)將「支持的界面面向」在我目標(XCode中),這樣我就可以使用所有方向

3)實施新的iOS 6.0旋轉功能

- (BOOL) shouldAutorotate { 

    return YES; 
} 


-(NSUInteger)supportedInterfaceOrientations{ 

    return UIInterfaceOrientationMaskAll; 
} 

4.)由於某些原因,我分類UINavigationController並實現了這些新功能,並使用這個新的NavigationController而不是原來的。

到目前爲止,一切正常,所有viewcontrollers現在都可以旋轉到每個方向。現在我想要幾個viewController不旋轉,只留在肖像。但是,當我在那些特定viewcontrollers設置新的輪換方法就是這樣,它仍然旋轉的每一個方向:

- (BOOL) shouldAutorotate { 

    return NO; 
} 


-(NSUInteger)supportedInterfaceOrientations{ 

    return UIInterfaceOrientationMaskPortrait; 
} 

還設置navigationController的rotationsfunctionality上面一樣沒有任何改變。 (所有viewcontrollers可以旋轉到每個方向)

我在做什麼錯?

編輯:

還設置首選Interfaceorientation不會改變任何東西:

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { 

    return UIInterfaceOrientationMaskPortrait; 
} 

回答

11

如果你希望所有的導航控制器的尊重頂視圖控制器可以使用一個類別。我發現它比子類更容易。

@implementation UINavigationController (Rotation_IOS6) 

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
} 

@end 
+0

謝謝,這解決了我的問題! – NicTesla

+5

小心使用此解決方案 - 如果視圖控制器的supportedInterfaceOrientations方法碰巧在導航控制器上調用supportedInterfaceOrientations,則您有無限遞歸。我發現蘋果的UIPrintingProgressViewController嚮導航控制器詢問支持接口方向的方式很難...... – tyler

+0

@tyler我們如何處理這個問題?打印導致無限遞歸。無論如何,我無法修復它。我使用上面的類別來旋轉我的應用程序中的特定視圖控制器。 –

0

這個工作對我來說:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return NO; 
} 
+2

感謝您的快速回復,但是此方法在iOS 6中已被刪除。在iOS 6中,您應該使用兩個方法shouldAutorotate和supportedInterfaceOrientations來代替。 – NicTesla

0

您需要創建的UITabBarController的類別應該支持自動翻轉

.h文件中的代碼是

@interface UITabBarController (autoRotate)<UITabBarControllerDelegate> 

    -(BOOL)shouldAutorotate; 
    - (NSUInteger)supportedInterfaceOrientations; 

@end 

.m文件的代碼是

-(BOOL)shouldAutorotate { 

    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
    return [delegate.tabBarController.selectedViewController shouldAutorotate]; 
} 


- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskAll; 
} 

注意:AppDelegate的名稱將隨您的項目的AppDelegate文件名更改。

+0

鏈接只有答案不歡迎在StackOverflow。 –

+0

垃圾郵件,自我推銷鏈接也不歡迎Stack Overflow。 –

相關問題