尽管设备旋转,layoutsubviews仅被调用一次 [英] layoutsubviews only called once despite any device rotation
问题描述
恰如其名:我创建一个空的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;