【问题标题】:Trying to understand Render Loop of AutoLayout in iOS试图理解 iOS 中 AutoLayout 的渲染循环
【发布时间】:2019-01-03 08:26:30
【问题描述】:

您好,我想了解一下 WWDC 2018 高性能自动布局中引入的 Auto Layout 的渲染循环

他们说当引擎(自动布局)计算约束方程然后引擎将值通知给视图

最后视图调用superview调用setNeedsLayout

所以我试着检查一下

这是我的看法

BlueView 是 SuperView,GreenView 是 Subview

然后我覆盖 setNeedsLayoutlayoutSubviewsdrawupdateConstraints 以检查方法调用的顺序

结果是这样的

除了 4 次 setNeedsLayout 通话和 Only Superview 通话 setNeedsLayout 外,我都能理解

这是我的问题

  1. 为什么只有 SuperView 调用setNeedsLayout
  2. 为什么在这种情况下setNeedsLayout 被调用了 4 次

这是我的代码

class SuperView: UIView {
    override func updateConstraints() {
        super.updateConstraints()
        print("SuperView : updateConstraints")
    }

    override func setNeedsLayout() {
        super.setNeedsLayout()
        print("SuperView : setNeedsLayout")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        print("SuperView : layoutSubviews")
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        print("SuperView : draw")
    }
}

class SubView: UIView {
    override func updateConstraints() {
        super.updateConstraints()
        print("SubView : updateConstraints")
    }

    override func setNeedsLayout() {
        super.setNeedsLayout()
        print("SubView : setNeedsLayout")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        print("SubView : layoutSubviews")
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        print("SubView : draw")
    }
}

【问题讨论】:

    标签: ios uiview autolayout


    【解决方案1】:

    AutoLayout Engine 在 layoutSubviews() 中从 AutoLayout Engine 模型(使用约束计算)中设置值

    setNeedsLayout 在 superview 中调用,因为某些东西触发了这个。我认为这是因为在此之后更改层次结构(添加子视图)您向子视图添加约束(这也触发 setNeedsLayout),更改约束的优先级也会触发此。

    • 添加或删除子视图
    • intrinsicContentSize 已更改
    • 激活或停用约束
    • 更改约束优先级

    这点在superview中触发setNeedsLayout()。

    希望对你有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      相关资源
      最近更新 更多