2012-12-07 102 views
2

我想要做的是最簡單的概念。但是,我只是沒有得到任何期望的結果。iPhone iOS 6 UITabBarController僅限肖像,如何讓Modal View Controller支持Landscape Orientation

我的應用程序是一個標準的選項卡欄應用程序,每個選項卡中的所有視圖控制器僅支持縱向方向,這正是我想要的。

但是,在應用程序的一個部分中,我顯示了一個模式視圖控制器,它顯然覆蓋了標籤欄控制器。這是一個文本輸入屏幕,我非常希望這個視圖能夠支持橫向和縱向。然後,一旦用戶取消了該模式視圖控制器,標籤欄控制器將再次顯示,所有內容都以縱向顯示。

我已經嘗試了很多東西,沒有任何工作。如果我告訴應用程序支持這兩種方向,那麼旋轉會在模式上正確發生,但也會在應用程序的其他部分上正確顯示,這是我不想要的。

我已經嘗試實施所有新的shouldAutorotate和supportInterfaceOrientations方法,而且似乎沒有任何工作。

最近的嘗試,我幾乎工作,是我創建了一個UITabBarController類別在我的應用程序委託,以承諾shouldAutorotate和supportedInterfaceOrientations。這似乎最初工作,但由於某種原因,每當取消我的模態vc,我的應用程序的標籤欄部分總是向上移動20像素後面的狀態欄?我不知道那是什麼。

我創建了一個測試應用程序,其中沒有UITabBarController,並且我能夠正確編碼所需的行爲,並且完美無缺。所以,顯然Tab Bar Controller方面的一些問題正在成爲一個難題。

請讓我知道解決這個簡單的概念是什麼技巧。

謝謝!

回答

2

顯然在ios6及以上版本中,旋轉工作的方式是不同的。所以你需要做的是以下幾點

  1. 在你的.plist支持所有4個方向。
  2. 子類的UITabBarController(用於如:CustomTabBarController)
  3. 在CustomTabBarController放碼

    -(NSUInteger)supportedInterfaceOrientations 
    { 
        return UIInterfaceOrientationMaskPortrait; 
    } 
    
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
    { 
        return UIInterfaceOrientationPortrait; 
    } 
    
  4. 以下線在你的應用程序代理或在任何你正在初始化的UITabBarController,更換CustomTabBarController這些實例實例。

  5. 在你的模態控制器放線

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
    { 
        return UIInterfaceOrientationLandscapeLeft; 
    } 
    
    -(BOOL)shouldAutorotate{ 
        return NO; 
    
    } 
    

,它應該所有的工作。

顯然,我發現的訣竅是,UITabBarController不會聽你的指示。它將支持你在.plist中提到的所有方向。

因此,您必須對其進行子類化。

我試着做所有上述,它工作正常。請讓我知道,如果您願意,我可以向您發送代碼。

+0

感謝您的意見,但我能夠拿出一個解決方案,使用類別,而我發佈爲答案。由於SO規則,我不能'接受'兩天的答案。 – bpatrick100

+0

這工作完美!有一點需要注意。如果您正在使用故事板,則必須將選項卡欄控制器上的自定義類更改爲您創建的自定義類。 – mikemike396

3

我能夠通過爲UITabBarController和UINavigationController創建幾個類別來解決這個問題。這裏是我使用的代碼:

@implementation UITabBarController (rotations) 

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 

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

@end 

@implementation UINavigationController (navrotations) 

- (BOOL)shouldAutorotate { 

    return [self.topViewController shouldAutorotate]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 

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

@end 

然後,當然每個視圖控制器I顯示將簡單需要到shouldAutorotate和supportedInterfaceOrientations方法作出響應。

相關問題