【问题标题】:UIViewController and UISplitViewController in UITabBarController shouldAutorotateToInterfaceOrientationUITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation
【发布时间】:2010-07-23 09:02:56
【问题描述】:

我的 iPad 代码有一些问题。

我有一个 UITabBarController,其中包含一些 UIViewController 和一个 UISplitViewController。问题是 UIViewController 甚至 UISplitViewController 都不能正确识别方向变化。

我已经在我的 TabBarController 和所有 UIViewControllers 上设置了 shouldAutorotateToInterfaceOrientation,但我意识到只有 Top moast ViewController 中的 willRotateToInterfaceOrientation 会触发,这是我的 TabBarController。如果我从我的 TabBarController 中删除 shouldAutorotateToInterfaceOrientation,我的子 UIViewControllers 中的 willRotateToInterfaceOrientation 将被调用。最大的问题是我的 UISplitViewController,因为它会旋转到新的 interfaceOrientation 却卡在他的 Portrait Layout 中。

如何正确实现带有 ViewControllers 和 Splitviews 的 TabBarController,包括方向变化?

【问题讨论】:

    标签: ipad uiviewcontroller uitabbarcontroller rotation uisplitviewcontroller


    【解决方案1】:

    嘿,我现在自己想出了一个解决方法。 重述问题 只有窗口的第一个附加视图才能识别方向更改。

    我将我的 TabBarController 子类化并使其旋转到界面方向

    - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [self adjustViewsForOrientation:toInterfaceOrientation];    
    }
    
    - (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
        if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
            NSLog(@"Landscape");
            //Do Your Landscape Changes here
    
    
        }
        else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
            NSLog(@"Portrait");
            //Do Your Portrait Changes here
        }
    }
    

    但是现在我的 TabBarController 的“viewControllers”仍然无法识别我的 InterfaceOrientations。所以我想出了以下内容:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        for (int i = 0; i < [self.viewControllers count]; i++ ) {
            [[self.viewControllers objectAtIndex:i] didRotateFromInterfaceOrientation:fromInterfaceOrientation];
        }
    }
    

    这将从 TabBarController 的所有子类调用 didRotateFromInterfaceOrientation 方法:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
        [self adjustViewsForOrientation:self.interfaceOrientation];
    }
    
    - (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
        if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
            NSLog(@"Subview Landscape");
            //Do Your Landscape Changes here
        }
        else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
            NSLog(@"Subview Portrait");
            //Do Your Portrait Changes here
        }
    }
    

    如您所见,我在子视图控制器中调用[self adjustViewsForOrientation:self.interfaceOrientation];,这将为调整方法提供实际的方向。如果您使用 fromInterfaceOrientation 将是错误的方向,因为更改已经完成!

    我的另一个问题是 TabBarController 中的 UISplitviewController,但我没有让它以可接受的方式工作。问题与 UIViewControllers 相同。它不会重新识别方向变化,所以你必须对它进行子类化,但我没有让它工作到 100%。当我在网上搜索时,我发现了一个很好的代码示例,用于 cutsom 构建 Splitview。所以你可能会试一试: http://blog.trustedones.com/development/ipad-uisplitviewcontroller-replacement-for-sethidesmasterviewinportrait http://www.trustedones.com/apps/ipad

    它还使 SplitView 保持在纵向模式,因此您可能会喜欢它。我愿意!

    希望我能在这篇文章中帮助某人.. 干杯 网民

    【讨论】:

    • 你有没有想过如何让它完全按照你的意愿工作?
    【解决方案2】:

    请注意,UITabBarController 类引用的第二句声明“该类不适用于子类化。”

    因此,虽然这种方法可能有效,但我怀疑它不是“正确”的方法。 (我也有这个问题。)

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 2011-01-29
      • 1970-01-01
      • 2016-02-13
      • 2023-03-05
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多