在嵌套的 UIStackView 中,当我添加多个排列的子视图时,它会破坏布局,为什么?

in nested UIStackView when I add more than one arranged subview, it breaks the layout, why?

玩 UIStackView 我遇到了一个奇怪的问题,它不允许我在嵌套的 UIStackView 中添加额外排列的子视图:

 override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        let subViews = [UIColor.gray, UIColor.darkGray, UIColor.lightGray].map { (color) -> UIView in
            let v = UIView()
            v.backgroundColor = color
            return v
        }

        let redView = UIStackView(arrangedSubviews: subViews)
        redView.distribution = .fillEqually
        redView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true


        let blueView = UIView()
        blueView.backgroundColor = .blue


        let buttons = [UIColor.gray, UIColor.darkGray, UIColor.lightGray].map { (color) -> UIView in
            let v = UIView()
            v.backgroundColor = color
            return v
        }

        let buttonsView = UIStackView(arrangedSubviews: buttons)
        buttonsView.distribution = .fillEqually
        buttonsView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true


        let stackView = UIStackView(arrangedSubviews: [redView, blueView, buttonsView])

        view.addSubview(stackView)
        stackView.axis = .vertical
        stackView.fillSuperview()

    }

结果,我得到了完全拉伸的蓝色视图,而不是预期的行为:

但是当我在底部堆栈视图中留下一个子视图时 - 它按预期显示

   let buttons = [UIColor.gray].map { (color) -> UIView in
        let v = UIView()
        v.backgroundColor = color
        return v
    }

    let buttonsView = UIStackView(arrangedSubviews: buttons)
    buttonsView.distribution = .fillEqually
    buttonsView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true


    let stackView = UIStackView(arrangedSubviews: [redView, blueView, buttonsView])

为什么?代码有什么问题?感谢任何帮助或提示,我也在顶部和底部堆栈视图上尝试 translatesAutoresizingMaskIntoConstraints = false 但没有任何运气

在底部堆栈视图中需要设置 buttonView.heightAnchor.constraint(equalToConstant: 100).isActive = true

而不是

buttonView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true