3

我的 iPad 代码有一些问题。

我有一个包含一些 UIViewController 和一个 UISplitViewController 的 UITabBarController。问题是 UIViewController 甚至 UISplitViewController 都无法正确识别方向更改。

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

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

4

2 回答 2

0

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

我将我的 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 保持在纵向模式,因此您可能会喜欢它。我愿意!

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

于 2010-07-25T22:12:56.000 回答
0

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

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

于 2010-08-28T00:50:42.903 回答