2012-09-30 21 views
17

在iOS6中,shouldAutorotateToInterfaceOrientation已棄用。我試圖使用supportedInterfaceOrientationsshouldAutorotate使應用程序正確工作,但失敗。如何使應用程序在iOS 6中完全正確地自動運行?

這個ViewController我不想旋轉,但它不起作用。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

任何想法? 感謝您提前提供任何幫助!

+0

視圖控制器是嵌入在導航控制器或tabbar控制器中嗎?嵌入導航控制器內的 – Felix

+0

。 @ phix23 – Carina

回答

38

想通了。

1)子類的UINavigationController(層次結構的頂部視圖 - 控制將採取定向的控制。) 沒有將其設置爲self.window.rootViewController。

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

2)如果你不想視圖控制器旋轉

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

3)如果你希望它能夠BTW旋轉

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

,根據根據您的需要,另有相關方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
+2

+1用於回答你自己的問題,用一些不錯的代碼示例 –

+0

我試過這種方法,它運行良好。 – Robert

+0

你爲什麼要覆蓋' - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation'?它已被棄用,從不呼叫 – voromax

3

如果您使用選項卡欄控制器而不是導航控制器作爲根控制器,則需要類似的子類UITabBarController。

此外,語法也會有所不同。我成功地使用了以下內容。然後我使用上面的例子,在我想覆蓋的視圖控制器上取得成功。在我的情況下,我想主屏幕不旋轉,但我有一個電影常見問題屏幕,我自然希望啓用橫向視圖。完美工作!只要注意語法更改爲self.modalViewController(如果您嘗試使用導航控制器的語法,則會得到編譯器警告。)希望這有助於!

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

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

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return self.modalViewController.supportedInterfaceOrientations; 
} 
相關問題