2010-09-10 196 views
0

我有一個TabbarController與不同的ViewControllers。 其中一些ViewController應該自動旋轉,並且一個ViewController只能在PORTRAIT中呈現。 我填充的TabBar以下程序:UIViewController旋轉問題

ChartThemeViewController *chartViewController = [[ChartThemeViewController alloc] 
                  initWithTheme:chartThemeDict]; 
UINavigationController *theNavigationController = [[UINavigationController alloc] 
                  initWithRootViewController:chartViewController]; 
[chartViewController release]; 
[localViewControllersArray addObject:theNavigationController]; 
[theNavigationController release]; 

tabBarController.viewControllers = localViewControllersArray; 

的ChartThemeViewController是ThemeViewController的子類。 ThemeViewController是UIViewController的子類。

如果我在ThemeViewController的子類中重寫'shouldAutorotateToInterfaceOrientation',並在所有子類中返回YES,並在ChartThemeViewController中返回NO ...它碰巧所有ViewController都不自動旋轉。

mmmmhhh ......希望你能明白這一點...

我怎樣才能解決這個問題?

許多感謝 延

回答

0

相信竅門的UITabBarController的實例內選擇性自轉如下:子類的UITabBarController和覆蓋的方法 - (BOOL)shouldAutorotateToInterfaceOrientation:用下列:

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

現在,如果您選擇僅肖像選項卡並旋轉到橫向,應用程序將保持縱向。

雖然這裏有一個新問題,如下所示:選擇一個標籤確實支持橫向;旋轉到風景;選擇一個僅支持肖像的選項卡。呃哦。橫向顯示僅肖像視圖控制器。不幸的是,沒有辦法強制視圖控制器到特定的方向。

你可以看看這裏的問題的描述:How to force a screen orientation in specific view controllers?

正如你所看到的,沒有真正的解決方案。

我建議禁用所有不支持當前方向的選項卡。您可以覆蓋 - (無效)didAutorotateToInterfaceOrientation:在你的UITabBarController的子類具有以下實現這一目標:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    NSArray *items = [[self tabBar] items]; 
    for (NSUInteger i = 0; i < [items count]; i ++) 
    { 
     UITabBarItem *item = [items objectAtIndex:i]; 
     if (![[[self viewControllers] objectAtIndex:i] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) 
     { 
      [item setEnabled:NO]; 
     } 
     else 
     { 
      [item setEnabled:YES]; 
     }; 
    }; 
}; 

如果你不希望這樣做,然後再考慮改變你的接口,能夠支持在同一方位你所有的視圖控制器。

希望這有助於

瑞安

+0

謝謝你這個答案。 – xnz 2010-09-13 09:16:45