在选项卡栏控制器的层次结构中旋转视图控制器 ->导航控制器->视图控制器
我的应用程序有一个像这样设置的视图控制器层次结构:
UITabBarController
|
UINavigationController
| |
| UIViewController
|
UINavigationController
|
UIViewController
此层次结构中的所有视图控制器都会覆盖该方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
并返回 YES
- 因此视图控制器应该能够旋转到任何旋转 -甚至颠倒。
然而,在此设置中,没有一个视图控制器能够成功旋转。我的印象是,如果导航和标签栏控制器的视图控制器响应旋转,它们就会旋转。
为什么我的视图控制器不旋转?
我能够让它们旋转的唯一方法是通过子类化 UINavigationController
并重写它的 shouldAutorotate
方法,但这对我来说没有必要,我想知道是否有什么东西我错过了这项工作。
编辑:
根据用户体验编码指南:
如果您还使用工具栏,则每个工具栏项的视图控制器必须实现 shouldAutorotateToInterfaceOrientation: 方法,并为您希望支持的每个方向返回 YES。如果您有工具栏项的导航控制器,则该导航控制器的根视图控制器必须实现 shouldAutorotateToInterfaceOrientation: 方法并返回 YES。
它说“工具栏” - 但我认为这是一个拼写错误,可能应该是“选项卡栏”。
所以看来我正确地实现了这一点,但我的控制器仍然不自动旋转。
My app has a view controller hierarchy set up like this:
UITabBarController
|
UINavigationController
| |
| UIViewController
|
UINavigationController
|
UIViewController
All of my view controllers that are within this hierarchy override the method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
and return YES
- therefore the view controller should be able to rotate to any rotation - even upside down.
However, within this setup none of the view controllers successfully rotate. I was under the impression that navigation and tab bar controllers would rotate if their view controllers respond to rotating.
Why won't my view controllers rotate?
The only way I've been able to get them to rotate is by subclassing UINavigationController
and overriding it's shouldAutorotate
method, but this feels unnecessary to me and I was wondering if there's something I've missed to make this work.
Edit:
According to the User Experience Coding How-to:
If you are also using a toolbar, the view controller for each toolbar item must implement the shouldAutorotateToInterfaceOrientation: method and return YES for each of the orientations you wish to support. If you have a navigation controller for a toolbar item, the root view controller of that navigation controller must implement the shouldAutorotateToInterfaceOrientation: method and return YES.
It says 'toolbar' - but I think this is a typo and is probably supposed to be 'tab bar'.
So it seems that I'm implementing this correctly, yet my controllers still do not auto rotate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到过这个问题,但我不记得它发生的确切原因。当询问特定方向时,选项卡栏控制器要求其所有视图控制器响应“是”,以便旋转到该方向。
如果以模态方式呈现,似乎与底层 VC 系统无关。
我创建了一个测试来展示这一点(GitHub 上的 RotationTest),但这一切似乎都有效。希望我能记得为什么我在某个时候失败了。
I've run into this problem, but I can't remember the exact reason it occurred. The tab bar controller requires all of its view controllers to respond YES when asked about a particular orientation for it to rotate to that orientation.
If presented modally, it seems like it doesn't matter about the underlying VC system.
I have created a test to show this (RotationTest on GitHub), but it all seems to be working. Hopefully I can remember why I was failing with this one at some point.
您是否尝试过对 Tabbarcontroller 进行子类化并将其设置为您的 tabbarcontroller?在那里,设置
为
Have you tried subclassing the Tabbarcontroller and setting it as your tabbarcontroller? In there, set
to
我不确定这是否是错误的方法,但我会比 UINavigationController 更快地对 UITabBarController 进行子类化。另外,您可以尝试将所有内容包装在实现旋转方法的子类 UIViewController 中,但这会产生额外视图的开销。我曾经尝试在 UINavigationController 中进行旋转,但效果并不好。我怀疑视图仅在您子类化 UINavigationController 时才旋转的原因是视图层次结构仅在父级旋转时才会传递旋转。如果父级不旋转,子级也不会旋转。 (想象一下 iPhone 在底座上。只有底座旋转,iPhone 才能旋转。现在,将底座与 iPhone 外壳进行比较。外壳也可以旋转,因此 iPhone 也会旋转。)
我不认为 HIG 在这方面有错字。这些术语可以互换。
通常,“工具栏”与包含它的视图相关,因此应与其父视图一起旋转。然而,可以说,选项卡栏是屏幕上视图控制器的“父级”。因此,视图控制器应该仅在整个应用程序旋转时才旋转。这个概念基本上可以归结为:哪个视图(栏或视图控制器)依赖于另一个? (选项卡栏是持久的,但视图会更改,或者工具栏仅在视图可见时才存在。)
I don't know for sure if this is the wrong approach, but I would subclass the UITabBarController, sooner than the UINavigationController. Also, you can try wrapping everything in a subclassed UIViewController that implements the rotation method, but this will create the overhead of an extra view. I once tried to do rotation in an with UINavigationController, but it was not pretty. I suspect that the reason the views only rotate if you subclass the UINavigationController is that the view hierarchy will only pass the rotation if the parent rotates. If the parent doesn't rotate, the child won't. (Imagine an iPhone in a dock. The iPhone only can rotate if the dock rotates. Now, compare the dock to an iPhone case. The case can also rotate, so the iPhone will rotate too.)
I do not think that the HIG has a typo in that regard. The terms may interchangeable.
Generally, a "toolbar" is relevant to the view that contains it, and therefore should rotate with its parent view. A tab bar, however, is the "parent", so to speak, of the view controller on the screen. The view controller should therefore only rotate if the entire app rotates. This concept basically boils down to this: Which view (bar or view controller) is dependent on the other? (The tab bar is persistent, but the views change, or is the toolbar only there if the view is visible.)
子类化 UITabBarController 和 UINavigationController。它的工作原理与使用 xCode 4.4 一样。
Subclass the UITabBarController as well as the UINavigationController. It works as using xCode 4.4.
我开发了一个扩展,允许您在不子类化 UITabBarController https://github.com/piercifani/TabBarBetterRotation< /a>
I have developed an extension that allows you to do just this without subclassing UITabBarController https://github.com/piercifani/TabBarBetterRotation