为什么 UIStackView 调整排列子视图的大小?

Why UIStackView resize arranged subviews?

为什么 UIStackView 调整子视图的大小?我认为当我将 distribution 设置为 .fill 时它起作用了。但是 .equalSpacing 并不意味着这种行为。我说的对吗?

let stackView = UIStackView()
    stackView.alignment = .firstBaseline
    stackView.axis = .vertical
    stackView.distribution = .equalSpacing
    stackView.spacing = 0.0
    stackView.translatesAutoresizingMaskIntoConstraints = true
    self.view.addSubview(stackView)

    stackView.snp.makeConstraints { (make) -> Void in
      make.top.equalTo(button.snp.bottom)
      make.left.equalTo(view).offset(16.0)
      make.bottom.equalTo(view)
      make.right.equalTo(view).offset(-16.0)
    }

    let exampleView = UIView(frame: .zero)
    stackView.addArrangedSubview(exampleView)
    exampleView.backgroundColor = .yellow

    exampleView.snp.makeConstraints { (make) in
      make.width.equalToSuperview()
      make.height.equalTo(60.0)
    }

一个UIStackView排列它的子视图。

这意味着:

  • 如果您限制其高度(高度限制或顶部和底部限制),它将调整/布局子视图以适应其高度。
  • 如果您限制其高度,它将使用其子视图高度进行布局。

请查看有关 UIStackViewDistributionEqualSpacing

的苹果文档

A layout where the stack view positions its arranged views so that they fill the available space along the stack view’s axis. When the arranged views do not fill the stack view, it pads the spacing between the views evenly. If the arranged views do not fit within the stack view, it shrinks the views according to their compression resistance priority. If there is any ambiguity, the stack view shrinks the views based on their index in the arrangedSubviews array.

https://developer.apple.com/documentation/uikit/uistackviewdistribution/uistackviewdistributionequalspacing?language=objc