美文网首页
layoutIfNeeded和setNeedsLayout的区别

layoutIfNeeded和setNeedsLayout的区别

作者: 走道牙的人 | 来源:发表于2016-06-17 14:47 被阅读680次

    - (void)layoutIfNeeded

    Description
    Lays out the subviews immediately.
    Use this method to force the layout of subviews before drawing. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root.

    - (void)setNeedsLayout

    Description
    Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
    Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.

    以上描述来自苹果官方文档,下面是总结:

    • layoutIfNeeded
      • 用于子控件包括父控件的frame设置完以后,调用该方法,会立即布局
    • setNeedsLayout
      • 用于还未设置父子控件的frame时,调用该方法,会在其frame被设置后,即恰当的时刻,自动完成布局

    相关文章

      网友评论

          本文标题:layoutIfNeeded和setNeedsLayout的区别

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