iOS 更新自动布局,获取子视图frame

XIB 自定义Cell 或 View,有时往往要拿到里面的子视图。再在子视图上面动态添加其它控件。

在工程中,获取到的一直是初始宽度或高度是在某一手机尺寸下。在另一个手机尺寸下,不起作用,还是当初的值,这就留下了bug.

解决办法是,视图在更新XIB约束时,会调用updateConstraints ,在这个方法再重设子视图frame!

- (void)updateConstraints

官网文档的解释:

Updates constraints for the view.
Custom views that set up constraints themselves should do so by overriding this method. When your custom view notes that a change has been made to the view that invalidates one of its constraints, it should immediately remove that constraint, and then call setNeedsUpdateConstraints to note that constraints need to be updated. Before layout is performed, your implementation of updateConstraints will be invoked, allowing you to verify that all necessary constraints for your content are in place at a time when your custom view’s properties are not changing.
You must not invalidate any constraints as part of your constraint update phase. You also must not invoke a layout or drawing phase as part of constraint updating.
Important:Important
Call [super updateConstraints] as the final step in your implementation.
---------------------  
 

你可能感兴趣的:(iOS_UI)