使用自定义 TabBarController 时不调用 shouldAutorotateToInterfaceOrientation
我创建了一个继承自 UIViewController 的自定义 TabBarController (不是 UITabBarController,因为它不适合子类化)。除了方向支持之外,一切正常。
我的 TabBarController 被设置为 UIWindow 上的 rootViewController,并包含 UIViewController 的内部数组。就我而言,我已添加 UINavigationControllers 作为每个选项卡的根。
当将新的 UIViewController 推送到 TabBarController 中的任何 UINavigationController 时,我会调用 shouldAutorotateToInterfaceOrientation,这一切都很好,因为在这里我可以设置哪个 UIViewController 应支持哪个方向(如 Apple 文档中所述)。
但是,当我按 UINavigationBar 中的后退按钮返回时,我没有收到对 shouldAutorotateToInterfaceOrientation 的调用,因此我们显示的视图最终将处于错误的方向。
我通过用 UITabBarController 替换我的自定义 TabBarController 进行了快速测试,按下后退按钮时我收到了对 shouldAutorotateToInterfaceOrientation 的调用,所以这里肯定有问题,但我不知道是什么问题。
为什么我没有收到对 shouldAutorotateToInterfaceOrientation 的调用? UITabBarController 是否做了我错过的事情?
这里有人遇到过同样的问题吗?你有什么想法可能值得尝试吗,因为我已经没有想法了。
编辑
通过使用 iOS5 容器视图控制器将 TabBarController 中的每个 UIViewController 添加为子级,可以解决此问题。
使用以下方法将 UIViewController 添加为子级: addChildViewController 然后覆盖automaticForwardAppearanceAndRotationMethodsToChildViewControllers 并返回YES。
现在你应该在 UIViewControllers 中获得 shouldAutorotateToInterfaceOrientation 回调
I have created a custom TabBarController that inherits from UIViewController (NOT UITabBarController because it is not intended for subclassing). Everything works fine except the orientation support.
My TabBarController is set as the rootViewController on my UIWindow and contains an internal array of UIViewControllers. In my case I have added UINavigationControllers as the root of each tab.
When pushing a new UIViewController to any of my UINavigationControllers in my TabBarController I get a call to shouldAutorotateToInterfaceOrientation, this is all fine because here I can set which UIViewController should support which orientation (as described in the Apple documentation).
However when I go back by pressing the back button in my UINavigationBar I do not get a call to shouldAutorotateToInterfaceOrientation hence the view we display will end up in the wrong orientation.
I have done a quick test by replacing my custom TabBarController with a UITabBarController and I get the call to shouldAutorotateToInterfaceOrientation when pressing the back button so there must be something wrong here but I cannot figure out what.
Why don't I get the calls to shouldAutorotateToInterfaceOrientation? Is the UITabBarController doing something I have missed?
Has anybody here experienced the same problem? Do you have any ideas that might be worth trying because I have run out of ideas.
EDIT
This issue is resolved by adding each UIViewController within the TabBarController as a child using the iOS5 container view controller.
Add the UIViewController as a child with this method: addChildViewController Then override automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers and return YES.
Now you should get the shouldAutorotateToInterfaceOrientation callbacks in your UIViewControllers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前见过这样的问题,它总是归结为向所有视图控制器添加
-shouldAutorotateToInterfaceOrientation:
方法,而不仅仅是最顶层的视图控制器正如你所期望的那样。(如果你很懒,只需向
UIViewController
添加一个类别即可)I have seen such problems before, and it always boiled down to adding the
-shouldAutorotateToInterfaceOrientation:
method to all view controllers, not just the topmost one as you might expect.(and if you're lazy, just add a category to
UIViewController
)