layoutSubviews相关信息

来源:互联网 发布:滑轮组实验数据 编辑:程序博客网 时间:2024/06/03 20:53

关于这个方法的英文描述是:

Lays out subviews.
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.

做一个简单介绍
布局subViews
在iOS5.1和之前的版本,默认执行这个方法是不做任何事的(do nothing)。如果你想让这个方法执行你所设定的布局约束条件,那么你就重写这个方法。那么这个方法在什么时候调用了?
以下几种方式:

  • init初始化不会触发layoutSubviews
  • addSubview会触发layoutSubviews
  • 设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
  • 滚动一个UIScrollView会触发layoutSubviews
  • 旋转Screen会触发父UIView上的layoutSubviews事件
  • 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
0 0