尽管设备旋转,layoutsubviews仅被调用一次 [英] layoutsubviews only called once despite any device rotation

查看:58
本文介绍了尽管设备旋转,layoutsubviews仅被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恰如其名:我创建一个空的UIView子类,并向其添加一个子视图,创建一个空的layoutSubviews方法,并在此添加断点.然后,我构建项目,并且在该时刻将视图加载到layoutSubviews方法中时,模拟停止.到目前为止一切都很好.我继续模拟并旋转设备(假设将视图控制器设置为允许任何方向),但是尽管我可以看到我的对象如何旋转,但从未再次调用layoutSubviews. 有什么主意吗?

just what the title says: I create an empty UIView subclass and I add a subview to it, I create an empty layoutSubviews method and I add a breakpoint there. Then I build the project and the simulation stops when loading the view at that point in the layoutSubviews method. Everything is fine so far. I continue the simulation and I rotate the device (assuming that the view controller is set to allow any orientation) but layoutSubviews is never called again, despite I can see how my object is rotating. Any idea?

好吧,我将简化我的问题:如何创建带有某些子视图的自定义UIView并使其响应设备方向?我不需要使用drawRect或其他任何东西,只需将UIView子类化,添加几个subViews并在设备旋转时对其进行处理即可.

OK, i will simplify my question: How do I create a custom UIView with some subviews and make it responsive to the device orientation? I don't need to use drawRect or anything, just subclass UIView, add a couple of subViews and handle them when the device is rotating.

推荐答案

您是否尝试在UIView上设置autoresizingMask?例如下面的例子:

Did you try setting the autoresizingMask on the UIView? For example like below:

someView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

当视图的边界发生变化时,该视图会自动调整其大小 每个子视图的自动调整大小蒙版的子视图"

'When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask'

如果将autoresizingMask设置为None,则意味着在视图范围更改时总是调用layoutSubviews方法.

If you set the autoresizingMask to something other than None it should mean that the layoutSubviews method is always called when the views bounds change.

这篇关于尽管设备旋转,layoutsubviews仅被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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