您的位置:首页 > 产品设计 > UI/UE

ios UIView layoutSubviews调用时机

2015-11-09 13:50 459 查看
iOS 官方文章中对UIView的layoutSubviews 介绍如下:

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.

大致意思是 iOS5.1之前这个方法没有任何作用。这个方法默认功能是控制一个子view的大小和位置

凡事继承UIView的子类都都可以实现这个方法,来控制子视图。实现次方法的前提是你觉得系统布局不能满足你的要求,你可以在这个方法里面做更为精确的设置

这个方法你无需调用。如果你想要更新layout的话 可以调用这个方法:setNeedsLayout  来实现。如果不想立刻刷新layout 你可以调用layoutIfNeeded 方法

下面我们研究下这个方法调用的时机

1.init初始化的时候是不会触发的。但是调用initWiftFrame 并且参数fram不为0的时候会调用.换句话就是大小或者位置更改的时候调用。

2,添加子视图的时候会调用(即调用addsubView方法的时候)

3,修改了frame的时候调用(1,也属于此类情况)

4,UIScrollView滚动的时候会调用此方法

5,屏幕旋转的时候会调用

6,子视图修改的时候会调用父级视图的此方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: