UITabBarController 和旋转

发布于 2024-07-14 01:57:44 字数 878 浏览 4 评论 0原文

我对 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

    I'm having a real issue with UITabBarController.
    The outcome I'm after is the following:
    1) in portrait mode, a simple tab bar based application (with navigation bars) nothing too fancy.
    2) in landscape mode, I want to use my own UIViewController ignoring the UITabBar completely.

    The approach (I tried many variants) I tried last which I fail to understand why is not "working" is the following:

  • I have a custom UIViewController (Call this AA) that is suppose to manage "everything".
  • This controller is added to the window in application start and in its loadView creates two controllers: a UITabBarController (Call this TBC) and a UILandscapeController (Call this LSC). then I add the tabbarcontroller view as a subview of AA's view.
  • now in AA class I override the didRotate blah or willRotate blah and basically want to switch between the two views, by this I means something like: (pseudo code):
    going from portrait to landscape:
  • [TBC.view removeFromSuperView];
    [AA.view addSubview:LSC.view];
    

    and when returning to portrait reverse it.

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

    The amount of problems I have (well, it simple rotates wrongly creating a real messed up interface) are something completely unexplained. It seems like the tabbarcontroller view does not "like" at all to be in the standard view heirarchy but rather it wants to be attached directly to the screen.
    I wonder what is the best approach to achieve my goal and why the tabbar does not like to be a subview of a view,

    any hints mostly appreciated.

    -t

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    蓝咒 2024-07-21 01:57:44

    以防万一您仍然需要答案,或者其他人偶然发现了这个问题,我已经做了同样的事情并使其正常工作,但是您必须克服一些困难。 为了旋转 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 在将根控制器的视图添加到窗口后立即被调用,因此您必须在完成后重新启用状态栏在您的应用程序委托中完成此操作。

    Just in case you still need the answer, or someone else stumbles onto this, I've done the same thing and got it working, but there are a couple of hoops you have to jump through. In order to rotate a UITabBarController's view, there are four things you have to do:

    1. Remove the status bar before switching to the view
    2. Rotate the view to the new frame
    3. Add the status bar back to the view
    4. Switch to the view.

    I've got a RootRotationController that does this that looks like this:

    @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;
        }
    }
    

    In addition, you should know that shouldAutorotateToInterfaceOrientation is called just after adding the root controller's view to the window, so you'll have to re-enable the status bar just after having done so in your application delegate.

    爱的故事 2024-07-21 01:57:44

    我认为你的问题来自于打字错误。 将removeFromSuperView 更改为removeFromSuperview。
    尽管如此,它仍然有一个问题。 标签栏无法正确旋转。 它向上移动,直到消失。

    不删除标签栏并使其透明怎么样?

    Your problem comes from the typo, I think. Change removeFromSuperView to removeFromSuperview.
    Though, it still has a problem. Tab bar doesn't rotate properly. It go upwards till it disappers.

    How about not removing the tab bar, and make it transparent.

    真心难拥有 2024-07-21 01:57:44

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

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

    Check out the UIViewController instance method rotatingFooterView in the docs.

    Or, you may manage TabBar by yourself, not through the UITabBarController.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文