子视图不会从横向旋转到纵向,反之亦然 [英] The child view does not rotate from landscape to portrait and vice versa

查看:47
本文介绍了子视图不会从横向旋转到纵向,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iPad 制作一个应用程序.我有一个必须保持横向的视图控件,这很好用.

I'm making an application for the iPad. I have a view controll that has to stay in landscape, this works just fine.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{
   return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

从这个视图我推向另一个视图,每当用户倾斜他的 ipad 时,这个子视图应该旋转到适当的旋转(横向或纵向).在这个子视图中,我使用此代码来实现这一点,但它不起作用.它坚持景观.

From this view i push towards an other view, this child view should rotate to the appropriate rotation (landscape or portrait) whenever the user tilts his ipad. in this child view i use this code to make this happen, but it doesn't work. It sticks to the landscape.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return YES;
}

推荐答案

为了让视图控制器支持旋转,其层次结构中的所有视图控制器都必须支持旋转.

In order for a view controller to support rotation, all view controllers in its hierarchy must support rotation.

来自 响应方向变化 文档:

窗口寻找合适的视图控制器并调用它的shouldAutorotateToInterfaceOrientation:方法来确定它是否支持新的方向.

The window looks for an appropriate view controller and calls itsshouldAutorotateToInterfaceOrientation:method to determine if it supports the new orientation.

容器视图控制器可能会拦截此方法并使用它们自己的启发式方法来确定是否应该发生方向更改.例如,标签栏控制器仅在其所有托管视图控制器都支持新方向时才允许方向更改.

Container view controllers may intercept this method and use their own heuristics to determine whether the orientation change should occur. For example, the tab bar controller allows orientation changes only if all of its managed view controllers support the new orientation.

此外,您不应使用多个视图控制器来管理单个屏幕.

Further, you should not be using multiple view controllers to manage a single screen.

来自 视图控制器编程指南 文档(强调我的):

From the View Controller Programming Guide documentation (emphasis mine):

视图控制器与其视图层次结构中的视图之间的一一对应是关键的设计考虑因素.您不应使用多个内容视图控制器来管理同一视图层次结构的不同部分.同样,您不应使用单个内容视图控制器对象来管理多个屏幕的内容.

The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple content view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single content view controller object to manage multiple screens’ worth of content.

在这种情况下,我建议在您的父视图控制器中禁用旋转处理,将子视图控制器更改为简单的视图(以满足上述条件),并手动监控方向更改以更新子视图的布局.

In this case, I'd suggest disabling rotation handling in your parent view controller, changing the child view controller to be simply a view (to meet the above criteria), and manually monitoring orientation changes to update your child view's layout.

您可以通过监听 UIDeviceOrientationDidChangeNotification 通知来监控方向变化.示例代码:

You can monitor for orientation changes by listening for the UIDeviceOrientationDidChangeNotification notification. Example code:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(yourSelector:)
    name:UIDeviceOrientationDidChangeNotification object:nil];

<小时>

更新

如果推送"是指将视图控制器推送到导航视图控制器上,请忽略我回复的第二部分.


Update

If by "push" you mean push a view controller onto a navigation view controller then please disregard the second part of my response.

如果是这种情况,您必须确保已覆盖导航控制器的 shouldAutorotateToInterfaceOrientation: 方法以返回 YES 以及您的视图控制器,以便支持轮换处理.

If this is the case, you must ensure that you have overridden the shouldAutorotateToInterfaceOrientation: method of your navigation controller to return YES as well as your view controller in order to support rotation handling.

这篇关于子视图不会从横向旋转到纵向,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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