如何实现UIViewController旋转以响应方向更改? [英] How to implement UIViewController rotation in response to orientation changes?

查看:100
本文介绍了如何实现UIViewController旋转以响应方向更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有大约10种不同的UIViewControllers,其中一种我想在旋转设备时切换到横向模式。 (所有其余的,我想保持纵向。)

My app has about 10 different UIViewControllers, just one of which I want to switch to landscape mode if the device is rotated. (All the rest, I want to keep in portrait.)

为了在那个视图上实现轮换,我需要实现其控制器的'shouldAutorotate'方法并返回是。由于这个视图是通过导航控制器访问的,我还需要创建一个UINavigationController的子类,它实现'shouldAutorotate'并返回YES。

In order to implement rotation on that one view, I needed to implement its controller's 'shouldAutorotate' method and return YES. Since this view is accessed via a navigation controller, I also needed to create a subclass of UINavigationController that implements 'shouldAutorotate' and return YES.

这个解决方案有效,但是太好了。我发现我推到UINavigationController的子类上的所有UIViewControllers都会响应旋转,即使我实现'shouldAutorotate'并返回NO。 (记住:我只希望一个特定的UIViewController响应旋转,而不是导航控制器堆栈中的每一个。)

This solution works, but too well. I find that all of the UIViewControllers I push onto my subclass of UINavigationController respond to rotation, even if I implement 'shouldAutorotate' and return NO. (Remember: I only want one particular UIViewController to respond to rotation, not every one in the navigation controller's stack.)

所以,我的问题是:我该如何做到最好这个?我能提出的所有解决方案似乎都很麻烦,2)更糟糕,似乎没有用。

So, my question is: how do I best do this? All the solutions I can come up with seem 1) cumbersome, and 2) worse, don't seem to work.

非常感谢。

推荐答案

当您实现UINavigationController时,此类是您控制将被推入堆栈的所有子viewControllers的父级。因此,RootViewController是唯一一个对自动旋转说是或否的控制器。即使您在子视图控制器中将Yes传递给自动旋转,它们也不计算在内!

when you implement UINavigationController, this class is your the parent that controls all the children viewControllers that will be pushed into the stack. Therefore, the RootViewController is the only controller that say Yes or No to autorotation. Even if, you are passing Yes to auto-rotation in children view controllers, they don't count!

这是UINavigationController的本质。所以要改变它,你有两个选择:

This is the nature of UINavigationController. So to change it, you have two choices:

1-手动操作它,这需要你通过一些繁琐的代码。

1- Manually manipulate it, which requires you to go through some cumbersome codes.

2-更改设计,使其与UINavigationController更兼容。应该旋转的单个视图应该由RootViewController调用(不是导航根视图控制器 - 它们的名称相同,但完全不同),即托管NavController的视图。当设备旋转时,它会将NavController推送到视图或另一个。

2- Change your design so that it is more compatible to UINavigationController. That single view that should rotate, should be get called by the RootViewController (not the Navigation Root View Controller -- they are named the same, but are totally different), the view that hosts NavController. And when the device rotates, it will either push the NavController to the view or the other.

3-另一种方法,它也可以工作,但是不推荐,因为它违反了MVC的概念,是你的NavController可以收听通知。 CAN和SHOULD旋转的特定子视图可以发出通知 - 例如rotateMe,一旦NavController听到它,它就会旋转。

正如我所说,它会起作用,但它违反了MVC模型 - 这很好到Apple,但不建议从编程角度出发。

As I said, it will work, but it violates the MVC model -- which is fine to Apple, but from programming perspective is not recommended.

如果您需要进一步解释其中任何一个,请告诉我。

If you need further explanation about either of them, please let me know.

这篇关于如何实现UIViewController旋转以响应方向更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆