解读LayoutSubviews

1.触发LayoutSubviews的几种情况

(1)init初始化的时候不会触发LayoutSubviews,view刷新不用LayoutSubviews方法,也没有这个方法

(2)addSubview时候会触发LayoutSubviews,前提是frame不是0

(3)设置view的frame会触发LayoutSubviews,前提是设置前后view的frame发生变化

(4)旋转Screen会触发父UIView上的LayoutSubviews

(5)改变一个UIView的大小的时候会触发父UIView的LayoutSubviews

2.LayoutSubviews的官方解释

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.

在iOS5.1或之前的版本中,这个方法什么也没干.这个方法的默认实现是用参数来设定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.

如果你需要更加精确的布局,可以在子类里面重写这个方法.仅仅在以下情况下:自动布局达不到你想要效果时你才有必要重写这个方法.你可以直接设置subviews的尺寸.

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.

你不能直接调用这个方法.如果你需要强制layout刷新,调用setNeedsLayout来代替.如果你想要立即刷新你的view,调用layoutIfNeeded

你可能感兴趣的:(解读LayoutSubviews)