0

我希望我的标签栏应用程序在横向模式下具有三个标签,但第三个标签必须是纵向的。我最终将整个应用程序转换为横向使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

在每个视图控制器中,作为选项卡栏应用程序都要求其所有子视图控制器都处于横向状态。

我的 .plist 文件首先具有横向选项,然后添加纵向。

如何使第三个选项卡旋转为纵向?

4

1 回答 1

2

The UITabBarController, sadly, doesn't handle view controllers with different rotation requirements very well. The best way to handle this is to subclass UITabBarController and in shouldAutorotate, simply pass the request on to the view controller that is on screen. The code would look like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Let's grab an array of all of the child view controllers of this tab bar controller
NSArray *childVCArray = self.viewControllers;

// We know that at 5 or more tabs, we get a "More" tab for free, which contains, assumingly,
// a more navigation controller

if (self.selectedIndex <= 3) {
    UINavigationController *navController = [childVCArray objectAtIndex:self.selectedIndex];

    // We're in one of the first four tabs, which we know have a top view controller of UIViewController
    UIViewController *viewController = navController.topViewController;

    return [viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else {
    // This will give us a More Navigation Controller

    UIViewController *viewController = [childVCArray objectAtIndex:self.selectedIndex];

    return [viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

return NO;
}

This assumes that you're 5+ view controllers use the tab bar's more navigation controller and are not themselves in their own uinavigationcontroller. If they were, altering this code to fit would be trivial.

I have posted this subclass up on my github, so you can copy this method or just grab the .h/.m files from here

于 2012-04-21T19:52:12.817 回答