ipad - UITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation

标签 ipad uiviewcontroller uitabbarcontroller rotation uisplitviewcontroller

我的 iPad 代码有一些问题。

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

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

如何正确实现具有 ViewController 和 Splitview 的 TabBarController(包括方向更改)?

最佳答案

嘿,我现在自己想出了一个解决方法。 回顾一下问题 只有窗口的第一个添加 View 才能识别方向更改。

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

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

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

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

希望我可以通过这篇文章帮助别人.. 干杯 内茨

关于ipad - UITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3316750/

相关文章:

ios - iPad 与 iPad Mini 用户代理

ios - 在 iOS 上的网页上使用键盘打开方向更改的缩放问题

ipad - 在工具栏上方显示键盘?

ios - 如何在另一个 UIViewController 之上呈现一个 UIViewController,但不占用全屏

ios - 呈现另一个 UIViewController 后关闭 UIViewController

ios - 选项卡索引在 App Delegate 类中正确,但在 App Delegate 对象中不正确

ios - 如何在UIScrollView上转到UIView的顶部

iphone - 将appdelegate数据传递给ViewController

ios - 如何隐藏导航 Controller 堆栈中单个 View Controller 的选项卡栏

iphone - 如何自定义UITabBarController的 'More'按钮?