美文网首页日常iOS基础
ios layoutsubview updateConstrai

ios layoutsubview updateConstrai

作者: 辣条少年J | 来源:发表于2021-07-10 23:20 被阅读0次

    一、layoutsubViews的调用时机

    1、init初始化不会触发layoutSubviews
    2、addSubview会触发layoutSubviews
    但是如果添加的子控件没有frame也不会调用
    3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
    4、滚动一个UIScrollView会触发layoutSubviews
    5、旋转Screen会触发父UIView上的layoutSubviews事件
    6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

    二、重要方法

    1、layoutsubviews

    layoutsubviews:这个方法默认没有做任何事,需要子类进行重写。

    2、setNeedsLayout

    setNeedsLayout:标记为需要刷新布局,不立即刷新,不会立即调用layoutsubviews

    3、layoutIfNeeded

    layoutIfNeeded:刷新setNeedsLayout标记的需要刷新单元,立即调用layoutsubviews方法

    4、setNeedsUpdateConstraints

    setNeedsUpdateConstraints:这个方法主要是用来标记当前控件是否需要调用updateConstraints 方法,只是标记,而不是调用。当给控件标记上需要刷新约束,如果程序没有直接强制刷新(调用updateConstraintsIfNeeded),那么会在系统RunLoop的下个周期开始时调用 updateConstraints 方法。

    5、updateConstraints

    updateConstraints:这个方法是更新约束方法,我们可以重写这个方法来添加约束,调用时机只能会有两种,一个是系统RunLoop的下个周期当发现需要更新布局(needsUpdateConstraints的值为YES)的时候,会自动调用该方法,或者是手动调用 updateConstraintsIfNeeded 来进行直接刷新。

    6、updateConstraintsIfNeeded

    updateConstraintsIfNeeded:直接强制立即调用 updateConstraints 方法,不需要等待Runloop下个周期,也不会管 needsUpdateConstraints 的返回值是否为YES。

    7、updateViewConstraints

    updateViewConstraints:调用时机是 self.view 的 needsUpdateConstraints 值为YES 或 [self.view updateConstraintsIfNeeded]; 都可。这个方法极大的方便了控制器本身的View的布局调整。

    相关文章

      网友评论

        本文标题:ios layoutsubview updateConstrai

        本文链接:https://www.haomeiwen.com/subject/prggultx.html