2012-11-29 14 views
2

我'目前工作的一個項目,我們有4個選項卡的標籤欄控制器,並在每個標籤有一個導航控制器。在每個導航控制器上都有多個視圖控制器。的TabBar控制器navigationcontrollers方向的ios 6

我看了很多帖子在這裏和其他地方,我們已經目前做了以下內容:

子類的UITabBarController

- (BOOL)shouldAutorotate 
{ 

    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController] shouldAutorotate]; 

} 

- (NSUInteger) supportedInterfaceOrientations 
{ 
    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController]supportedInterfaceOrientations]; 
} 

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

這項工作很好,如果我們在我們每個viewcontrollers的指定如下:

- (NSUInteger) supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    return YES; 
} 

這會將其鎖定到肖像,如預期。

但現在出現真正的問題! 如果我們在我們的視圖控制器中的其中一個選項卡上指定它應該旋轉到橫向,它可以正常工作,但是當我們更改選項卡時它仍然處於橫向模式,這不是我們想要的!

所以總結起來,有任何人有一個解決方案,你如何鎖定幾乎所有的意見,以給定的方向期待之一,而且可以改變標籤,他們是在你指定的方向(縱向這裏)?

我也讀過這篇文章iOS 6 UITabBarController supported orientation with current UINavigation controller,但正如一個評論也提到「這幾乎爲我工作。問題是,如果我已經在景觀中,當我切換標籤到肖像只查看它仍然在風景。肖像修復它,它不會轉回的風景,但我仍然需要在肖像首次加載時」幾乎是同樣在這裏..

回答

2

我自己也有這個問題,我研究出瞭解決辦法,它不漂亮,但它的作品。

在你TabbarController子類中實現此tabbarcontroller委託功能(記得設置代表):

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ 
    int selected = self.selectedIndex; 
    UIViewController *con = [[UIViewController alloc] initWithNibName:@"XIBName" bundle:nil]; 
    [[self.viewControllers objectAtIndex:selected] pushViewController:con animated:NO]; 
    [[self.viewControllers objectAtIndex:selected]popViewControllerAnimated:NO]; 
    [[self.viewControllers objectAtIndex:selected] setDelegate:nil]; 
} 

push和pop在標籤中的UINavigationController的navigationcontroller將使tabbarcontroller再次觸發其取向的功能,如果您正確實施了方向代碼,它將更改爲您所需的方向。

我希望這有助於,請下跌自由評論,如果我需要解釋任何細節。

最好的問候 莫滕

+0

那麼它可能不是一個漂亮,但它的魅力! 我現在可以將一個視圖切換到風景,而其他人仍然保持縱向,即使切換標籤! 非常感謝您的幫助! 正如你所說,設置委託很重要,我在viewwillappear中做了我的子類,並按預期工作! 希望這也能幫助其他人。 – Lasse

+0

嗯,這是不是與iOS的視圖控制器添加到標籤欄控制器的內部內存管理?我們遇到的問題是我們的整個應用程序都是故事式的,這意味着我們無法用代碼初始化視圖控制器。對於使用Storyboard的項目,是否有解決問題的方法? – jowie

+0

@jowie:你可以添加一個空的視圖控制器到你的故事板,分配一個故事板ID,然後調用[自我。storyboard instantiateViewControllerWithIdentifier:@「EmptyVC」]。 –