【问题标题】:UITabBarController and rotationUITabBarController 和旋转
【发布时间】:2009-02-14 11:57:05
【问题描述】:

我在使用 UITabBarController 时遇到了真正的问题。 我追求的结果如下: 1) 在纵向模式下,一个简单的基于标签栏的应用程序(带有导航栏)没什么太花哨的。 2) 在横向模式下,我想使用自己的 UIViewController 完全忽略 UITabBar。

我最后尝试的方法(我尝试了许多变体)我不明白为什么不“工作”如下:

  • 我有一个自定义 UIViewController(称为此 AA),它假设管理“一切”。
  • 此控制器在应用程序启动时添加到窗口,并在其 loadView 中创建两个控制器:一个 UITabBarController(调用此 TBC)和一个 UILandscapeController(调用此 LSC)。然后我将 tabbarcontroller 视图添加为 AA 视图的子视图。
  • 现在在 AA 类中,我覆盖了 didRotate blah 或 willRotate blah 并且基本上想要在两个视图之间切换,我的意思是:(伪代码): 从纵向到横向:
  • [TBC.view removeFromSuperView]; [AA.view addSubview:LSC.view];

    当返回纵向时反转它。

    [LSC.view removeFromSuperView]; [AA.view addSubview:TBC.view];

    我遇到的问题数量(嗯,它简单地旋转错误地创建了一个真正混乱的界面)是完全无法解释的。似乎 tabbarcontroller 视图根本不“喜欢”处于标准视图层次结构中,而是希望直接附加到屏幕上。 我想知道实现我的目标的最佳方法是什么以及为什么标签栏不喜欢成为视图的子视图,

    任何提示都非常感谢。

    -t

    【问题讨论】:

    标签: iphone uitabbarcontroller rotation


    【解决方案1】:

    以防万一您仍然需要答案,或者其他人偶然发现了这个问题,我已经做了同样的事情并让它发挥了作用,但是您必须跳过一些障碍。为了旋转 UITabBarController 的视图,您必须做四件事:

    1. 在切换到视图之前移除状态栏
    2. 将视图旋转到新框架
    3. 将状态栏添加回视图
    4. 切换到视图。

    我有一个 RootRotationController,它看起来像这样:

    @implementation RootRotationController
    
    #define degreesToRadian(x) (M_PI * (x) / 180.0)
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if ((UIInterfaceOrientationPortrait == interfaceOrientation) || (UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation)) {
            [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
        }
        // Return YES for supported orientations
        return YES;
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
        [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];
        if (UIInterfaceOrientationLandscapeLeft == interfaceOrientation) {
            self.view = self.landscape.view;
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
            self.view.bounds = CGRectMake(0, 0, 480, 300);
        } else if (UIInterfaceOrientationLandscapeRight == interfaceOrientation) {
            self.view = self.landscape.view;
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
            self.view.bounds = CGRectMake(0, 0, 480, 300);
        } else if (UIInterfaceOrientationPortrait == interfaceOrientation) {
            mainInterface.view.transform = CGAffineTransformIdentity;
            mainInterface.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
            mainInterface.view.bounds = CGRectMake(0, 0, 300, 480);
            [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
            self.view = mainInterface.view;
        } else if (UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) {
            mainInterface.view.transform = CGAffineTransformIdentity;
            mainInterface.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
            mainInterface.view.bounds = CGRectMake(0, 0, 300,480);
            [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
            self.view = mainInterface.view;
        }
    }
    

    此外,您应该知道 shouldAutorotateToInterfaceOrientation 是在将根控制器的视图添加到窗口后立即调用的,因此您必须在应用程序委托中重新启用状态栏。

    【讨论】:

      【解决方案2】:

      我认为您的问题来自拼写错误。将 removeFromSuperView 更改为 removeFromSuperview。 尽管如此,它仍然有问题。标签栏无法正确旋转。它会向上直到消失。

      不移除标签栏怎么样,让它透明。

      【讨论】:

        【解决方案3】:

        查看文档中的UIViewController 实例方法rotatingFooterView

        或者,您可以自己管理 TabBar,而不是通过UITabBarController

        【讨论】:

        • 感谢您的提示!但是我不明白为什么我建议的方法不起作用。我确信必须有一种方法来创建我所寻求的界面,它非常基本。我错过了一些简单的东西。我不认为这个问题需要创建一个完整的 TabBar 管理器。还有其他建议吗?
        猜你喜欢
        • 2010-11-06
        • 2012-03-27
        • 1970-01-01
        • 2012-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 2010-11-14
        相关资源
        最近更新 更多