UIScrollView 内的 UIStackView,增长到最大高度然后滚动

UIStackView inside UIScrollView, grows until maximum height then scrolls

我希望构建一个视图,该视图会随着向其添加子视图而增长。起初高度将为零,然后随着我添加子视图而增长,直到达到最大尺寸,然后我希望它变得可滚动。

如果我使用 UIScrollView 中的 UIView 构建它并手动设置从上到下的约束,它会按预期工作。但是,我觉得这应该可以使用 UIStackView,但是当我尝试滚动视图不会增长,或者 contentSize 的 scrollView 卡在最大高度时,不会滚动。我已经玩了一段时间,认为它实际上可能是 UIStackView 和 Autolayout 的一个错误,但希望我只是错过了一些东西。将 UIStackView 包装在容器 UIView 中没有帮助。

这是一个显示问题的完整视图控制器(点击任何地方都会向滚动视图添加标签)

import UIKit

class DumbScrollStack: UIViewController {

    let stack = UIStackView()
    let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Setup
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(addLabel)))
        stack.axis = .vertical
        view.backgroundColor = .blue
        scrollView.backgroundColor = .red

        //Add scroll view and setup position
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

        //Set maximum height
        scrollView.heightAnchor.constraint(lessThanOrEqualToConstant: 100).isActive = true

        //add stack
        scrollView.addSubview(stack)
        stack.translatesAutoresizingMaskIntoConstraints = false

        //setup scrollView content size constraints
        stack.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        stack.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
        stack.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
        stack.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
        stack.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true

        // Keep short if stack height is lower than scroll height
        // With this constraint the scrollView grows as expected, but doesn't scroll when it goes over 100px
        // Without this constraint the scrollView is always 100px tall but it does scroll
        let constraint = scrollView.heightAnchor.constraint(equalTo: stack.heightAnchor)
        constraint.priority = UILayoutPriority(rawValue: 999)
        constraint.isActive = true

        addLabel()
    }

    @objc func addLabel() {
        let label = UILabel()
        label.backgroundColor = .gray
        label.text = "Hello"
        stack.addArrangedSubview(label)
    }
}

您是否考虑过使用 UICollectionView?这听起来像是您要重建的内容。

确保将 "Scrolling Enabled" 设置为 true 并设置滚动方向。

添加这个

label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: .vertical)

或评论此版块

let constraint = scrollView.heightAnchor.constraint(equalTo: stack.heightAnchor)
constraint.priority = UILayoutPriority(rawValue: 999)
constraint.isActive = true

或使其低于 750(标签的默认垂直压缩)

constraint.priority = UILayoutPriority(rawValue: 749)

问题是标签的垂直压缩优先级低于 999,这总是使堆栈等于滚动视图的高度,因此没有滚动