UIControllers的选择性自转 [英] Selective Autorotation of UIControllers

查看:109
本文介绍了UIControllers的选择性自转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些自动旋转实验。
情况:我有一个TabBar 4(制表符),三个应该只是肖像。最后一个是UINavigationController,它本身不应该自动旋转任何堆栈的控制器。它基本上是一个浏览应用程序,因为我显示文件和文件夹一切都应该是纵向。有时,一个特殊的UIViewController被推送,我想只有这一个自动旋转(它总是最后在堆栈)。

I did some experiments on autorotation. The situation: I have a TabBar 4(tabs), three should be portrait only. The last one is a UINavigationController, which by itself should not autorotate any of the stacked controllers. It is basically a browsing application, as I show file and folders everything should be portrait. Some times, a special UIViewController is pushed, and I would like only this one to autorotate (it is always the last on the stack). In this last view, the tabbar is hidden.

我如何实现目标:我对UITabBarController进行了子类化,以覆盖标准的shouldAutorotate方法行为:

How I achieved the goal: I subclassed the UITabBarController, to override the standard shouldAutorotate method behaviour:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if([self.selectedViewController isKindOfClass:[UINavigationController class]])
        return [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    else
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];

这样,shouldAutorotate的答案被转发到受控制的选项卡,特别是对于UINavigationController,它再次委派给可见的UIViewController。基本上这是工作,因为我有所有的UIViewControllers回答否,除了上面描述的特定的一个:正确,当旋转模拟器,只有当特殊的UIViewController可见,接口旋转到横向,whici是完美的。 Tabbar在这里是隐藏的,所以用户不会得到那也是旋转(这在我的设计是不一致的:基本上当Tabbar可见,这意味着除了在这个特殊的视图,应用程序只是肖像的任何地方)。

This way, the answer of shouldAutorotate is forwarded to the controlled tabs, and in particular for the UINavigationController, it is again delegated to the visible UIViewController. Basically this works, as I have all the UIViewControllers answering NO, except for the particular one I described above: correctly, when rotating the Simulator, only when the special UIViewController is visible, the interface rotates to landscape, whici is perfect. The Tabbar here is hidden, so user don't get that also that one is rotated (which would be unconsistent in my design: basically whenever the tabbar is visible, which means everywhere except in this special view, the application is portrait only).

问题是,我想这样,即使设备仍然是横向模式,用户弹出特殊的ViewController,接口应该表现一致,并返回到纵向模式。相反,当我pop,界面停留在横向(它不是那样的设计,所以它是一个混乱,当然),即使当显示一个UIViewController,将回答NO到shouldAutorotate ...这是因为(我认为)的方法是只有当旋转发生时,才会调用,因此直到旋转实际上再次发生,界面将旋转到横向。

The problem is that I would like that, even if the device is still in landscape mode and user pops the special ViewController, the interface should behave consistently and return to portrait mode. Instead, when I pop, the interface stays in landscape (it's not designed in that way so it's a mess, of course) even when showing a UIViewController that would answer NO to shouldAutorotate... this is because (I think) the method is called only when rotation occurs, so until the rotation actually occurs again, the interface is rotated to landscape anyway.

如何避免这种情况?我的第一个解决方案将以某种方式拦截最后一个视图的弹出,并手动旋转视图之前弹出...但我不知道,我希望有一些更鲁棒的方法来处理!!

How do avoid this? My first solution would be somehow to intercept the popping of the last view, and rotate manually the view before popping... but I'm not sure, I hope there is some more robust method to handle!!

我使用模拟器3.0,dunno如果这有区别。

I use the simulator with 3.0, dunno if this makes a difference.

推荐答案

和我一样的问题,我解决了这个问题:
我继承了 UITabBarController ,并添加了以下代码:

I had the same problem as you, and I solved this way: I subclassed the UITabBarController, and added the following code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (self.selectedViewController)
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    else
        return (interfaceOrientation == UIDeviceOrientationPortrait);
}

这样,每个子视图控制器都可以控制自己的方向。

This way, every child view controller could control its own orientation.

这篇关于UIControllers的选择性自转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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