iOS 6 UITabBarController支持使用当前UINavigation控制器的方向

时间:2012-09-19 23:57:05

标签: uinavigationcontroller rotation uitabbarcontroller orientation ios6

我有一个iPhone应用程序我正在更新到iOS 6,它有旋转问题。我有一个UITabBarController,其中包含16 UINavigationCotrollers。大多数子视图可以在纵向或横向上工作,但其中一些只是纵向。对于iOS 6,它们不应该旋转。

我尝试将tabBarController子类化,以返回当前navigationController所选viewController的supportedInterfaceOrienations

- (NSUInteger)supportedInterfaceOrientations{

    UINavigationController *navController = (UINavigationController *)self.selectedViewController;
    return [navController.visibleViewController supportedInterfaceOrientations];
}

这让我更接近了。视图控制器在可见时不会旋转到位置,但如果我处于横向和切换选项卡中,即使不支持,新选项卡也将处于横向状态。

理想情况下,应用程序仅在当前可见视图控制器的支持对象中。有什么想法吗?

3 个答案:

答案 0 :(得分:58)

对您的UITabBarController进行子类化,覆盖这些方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

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

-(BOOL)shouldAutorotate
{
    return YES;
}

对您的UINavigationController进行子类化,覆盖这些方法:

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

-(BOOL)shouldAutorotate
{
    return YES;
}

然后在您不想旋转的viewControllers中实现这些方法:

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

对于您想要旋转的viewControllers:

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

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }

您的tabbarController应添加为应用程序窗口的RootviewController。如果您打算支持默认方向,除iPhone上的所有内容都是默认方式,那么您无需执行任何其他操作。如果您想要支持颠倒或者如果您不想支持其他方向,则需要在app delegate和/或info.plist中设置适当的值。

答案 1 :(得分:3)

我认为这样的事情更好(作为一种类别方法)

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

这确保了该方法的实现。如果您没有进行此检查并且该方法未实现(如在iOS5环境中),应用程序应该崩溃!

答案 2 :(得分:0)

如果您计划为所有视图控制器启用或禁用旋转,则不需要继承UINavigationController。   而是使用:

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

AppDelegate

如果您计划支持应用中的所有方向,但PARENT视图控制器(例如UINavigationController堆栈)的方向不同,则应使用

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

来自AppDelegate并结合您的PARENT视图控制器中的以下方法。

    - (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

但是如果您计划在同一个导航堆栈(例如我)中的不同CHILDREN ViewControllers中使用不同的方向设置,则需要检查导航堆栈中的当前ViewController。

我在UINavigationController子类中创建了以下内容:

    - (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    int interfaceOrientation = 0;

    if (self.viewControllers.count > 0)
    {
        DLog(@"%@", self.viewControllers);
        for (id viewController in self.viewControllers)
        {
            if ([viewController isKindOfClass:([InitialUseViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else if ([viewController isKindOfClass:([MainViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else
            {
                 interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }
    return interfaceOrientation;
}

由于您无法再控制子视图控制器所呈现的视图控制器的旋转设置,您必须以某种方式拦截视图控制器当前在导航堆栈中的内容。这就是我做的:)。希望有所帮助!