UINavigationController and autorotation(UINavigationController 和自动旋转)
问题描述
我有一个 UIViewController,它在 shouldAutorotateToInterfaceOrientation:
中返回 YES
用于 UIDeviceOrientationPortrait
和 NO
用于其他一切.使用堆栈顶部的视图,我使用 pushViewController:animated:
推送一个新的 UIViewController
.对于 shouldAutorotateToInterfaceOrientation:
中的任何内容,新控制器都会返回 YES
.
I have a UIViewController that returns YES
in shouldAutorotateToInterfaceOrientation:
for UIDeviceOrientationPortrait
and NO
for everything else. With that view on the top of the stack, I use pushViewController:animated:
to push a new UIViewController
. The new controller returns YES
for anything in shouldAutorotateToInterfaceOrientation:
.
第一个视图拒绝旋转(如预期的那样).推送第二个视图后,用户可以旋转设备,并且 UI 将旋转(也如预期的那样).如果第二个视图处于横向模式并且用户按下后退按钮(调用 popViewControllerAnimated:
),第一个视图将出现旋转(意外!).
The first view refuses to rotate (as expected). Once the second view is pushed, the user can rotate the device and the UI will rotate (also as expected). If the second view is in landscape mode and the user presses the back button (which calls popViewControllerAnimated:
), the first view will appear rotated (unexpected!).
如果用户将设备旋转回纵向,则视图将旋转,然后像以前一样停留在纵向模式.这可行,但在用户旋转回来之前,这对用户来说很难看.所以我正在寻找一种方法让这个视图保持纵向模式.
If the user rotates the device back to portrait orientation, the view will rotate and then be stuck in portrait mode as before. This works but it's ugly for the user until they rotate back. So I'm in search of a way to make this view stay in portrait mode.
到目前为止,我发现的唯一解决方法是使用 -[UIDevice setOrientation:]
,它会引发警告(orientation
是只读的)但因为它有效实际上是定义的.这是一个巨大的黑客,我想要一个真正的解决方案.为了寻找真正的解决方案,我将 GDB 附加到照片应用程序 (MobileSlideshow.app) 并发现它也使用 -[UIDevice setOrientation:]
.作为一个内部应用程序,虽然我猜他们有不同的规则.
The only workaround I have found so far is to use -[UIDevice setOrientation:]
, which throws a warning (orientation
is read-only) but works since it is actually defined. This is a huge hack and I'd like a real solution. In search of a real solution I attached GDB to the Photos application (MobileSlideshow.app) and discovered that it too uses -[UIDevice setOrientation:]
. Being an internal application though I guess they have different rules.
是否有正确的方法来实现预期的自转行为?
Is there a correct way to achieve the expected autorotation behavior?
推荐答案
我正要告诉你,可能没有办法,但后来我想到了.很难做到正确,但是如果您使用两个单独的 UINavigationController
可能会使其工作:一个控制根视图并禁止旋转,另一个用于允许子视图它.您将手动处理与根控制器和子控制器之间的转换.
I was about to tell you that there was probably no way, but then I had a thought. It would be difficult to get right, but you might be able to make it work if you used two separate UINavigationController
s: one that controls the root view and prohibits rotation, and one for the child views that allows it. You would manually handle the transition to and from the root controller and the child controller.
您必须修补子导航控制器才能拥有正确的后退按钮.而且,当然,您必须自己处理后退按钮.您可能必须使用虚拟 UINavigationBar
来制作从一个导航控制器到下一个导航控制器的动画,以便过渡看起来正确.您还必须为导航控制器之间的推送"过渡设置动画,这可能需要进行一些调整才能使其看起来正确.你必须:
You'd have to patch up the child navigation controller to have the correct back button. And, of course, you'd have to handle the back button press yourself. You would probably have to use a dummy UINavigationBar
to do the animation from one navigation controller to the next so that the transition will look right. You would also have to animate the "push" transition between the navigation controllers, as well, which might take a bit of tweaking to get it to look right. You would have to:
- 配置一个虚拟导航栏以与传出导航控制器完全匹配,并将其直接放在导航控制器栏的顶部.(您可以复制当前视图控制器的
UINavigationItem
的配置并推送它) - 将新的导航控制器放置在屏幕外的右边缘
- 为新旧控制器的帧从右向左移动动画
- 为传入的视图控制器创建
UINavigationItem
的副本并将其推送到虚拟导航栏上 - 动画完成后,从视图中移除虚拟
UINavigationBar
以及传出导航控制器.
- Configure a dummy navigation bar to exactly match the outgoing navigation controller's and place it directly on top of the navigation controller's bar. (You could copy the configuration of the current view controller's
UINavigationItem
and push it on) - Place the new navigation controller off-screen at the right edge
- Animate the movement of the new and old controllers' frames from right to left
- Create a copy of the
UINavigationItem
for the incoming view controller and push it on the dummy navigation bar - When the animation completes, remove the dummy
UINavigationBar
from the view, and also the outgoing navigation controller.
所有这一切都需要大量工作,但如果您非常聪明(并且非常顽强),您也许可以让它发挥作用.我很想看看结果!
All of this is a lot of work, but if you're very clever (and very tenacious), you might be able to get it to work. I'd love to see the result!
也就是说,您最好只使用 setOrientation:
并在 App Store 批准流程中把握机会;-)
That said, you might be better off just using setOrientation:
and taking your chances with the App Store approval process ;-)
这篇关于UINavigationController 和自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UINavigationController 和自动旋转
基础教程推荐
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01