美文网首页
layoutSubviews、layoutIfNeeded、se

layoutSubviews、layoutIfNeeded、se

作者: 我一不小心就 | 来源:发表于2019-01-23 09:41 被阅读0次

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

  1. 不要直接调用,如果需要强制更新(即视图更新立即起作用)则需要调用layoutIfNeeded,如果需要其在下一次视图更新的时候起作用则需要调用setNeedsLayout这个方法
  2. 子类可以根据需要覆盖此方法,以执行其子视图的更精确布局。仅当子视图的自动调整大小和基于约束的行为不提供所需的行为时,才应覆盖此方法。您可以使用实现直接设置子视图的框架矩形。
    总结:简而言之,就是调整控件内部子视图的位置可以调用此方法

setNeedsLayout:(官方说明如下)

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:(官方文档如下)

Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root. If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.

翻译如下:
使用此方法强制视图立即更新其布局。使用“自动布局”时,布局引擎会根据需要更新视图的位置,以满足约束的更改。使用以根视图接收消息的视图,此方法从根开始布局视图子树。如果没有待处理的布局更新,则此方法退出而不修改布局或调用任何与布局相关的回调。
总结:一般使用setNeedsLayout来标记某个视图的位置需要更新,然后再调用layoutIfNeeded方法强制刷新视图中子控件的布局.

相关文章

网友评论

      本文标题:layoutSubviews、layoutIfNeeded、se

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