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



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

如果我在 UIScrollView 中使用 UIView 构建它并手动从上到下设置约束,它会按预期工作。但是,我觉得使用 UIStackView 应该可以做到这一点,但是当我尝试滚动视图没有增长,或者滚动视图的内容大小卡在最大高度,并且不会滚动。我已经玩了一段时间了,认为它实际上可能是 UIStackView 和自动布局的错误,但希望我只是错过了一些东西。将 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)
    }
}

添加这个

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,这始终使堆栈等于滚动视图高度,因此没有滚动

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

确保将"已启用滚动"设置为 true 并设置滚动方向。

相关内容

最新更新