UIScrollView 中的嵌套 UIStackView 子项不会拉伸以填充

Nested UIStackView child in UIScrollView does not stretch to fill

我有一个UIStackView

我添加了一些观点。

最后一个视图应该在屏幕底部。为了实现这一点,我添加了一个充当 spacer 的视图,我在第一个和最后一个视图上设置了一个高度,中间视图拉伸以填充 space.

这有效。

    let topView = UIView(frame: .zero)
        topView.withSize(.init(width: 0, height: 100))
        topView.backgroundColor = .lightGray

        let spacerView = UIView(frame: .zero)
        spacerView.backgroundColor = .darkGray

        let bottomView = UIView(frame: .zero)
        bottomView.withSize(.init(width: 0, height: 200))
        bottomView.backgroundColor = .lightGray

        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.alignment = .fill
        stackView.spacing = 0
        stackView.distribution = .fill

        addSubview(stackView)

        stackView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
        ])

        [topView, spacerView, bottomView].forEach { stackView.addArrangedSubview([=10=]) }

我有一个场景,但是屏幕尺寸可能小于视图尺寸。

我正在尝试将我的 UIStackView 嵌入到 UIScrollView 中,但是当我这样做时,spacer 视图不再自行伸展。就好像高度现在为零。 (我添加了间距以显示顶部和底部视图)


        let topView = UIView(frame: .zero)
        topView.withSize(.init(width: 0, height: 100))
        topView.backgroundColor = .lightGray

        let spacerView = UIView(frame: .zero)
        spacerView.backgroundColor = .darkGray

        let bottomView = UIView(frame: .zero)
        bottomView.withSize(.init(width: 0, height: 200))
        bottomView.backgroundColor = .lightGray


        let scrollView = UIScrollView(frame: .zero)
        addSubviews(scrollView)

        NSLayoutConstraint.activate([
            scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
            scrollView.topAnchor.constraint(equalTo: topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])

        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.alignment = .fill
        stackView.spacing = 8
        stackView.distribution = .fill
        scrollView.addSubview(stackView)

        stackView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            // Attaching the content's edges to the scroll view's edges
            stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),

            // Satisfying size constraints
            stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
        ])

        [topView, spacerView, bottomView].forEach { stackView.addArrangedSubview([=11=]) }

在这种情况下,我希望我的 UIStackView 仍然填充视图,并且仅当子视图导致其高度增长超出视图的可见边界时才可滚动。

使用带有 UICollectionView 的 UITableView 作为页脚。

您可以在此处找到教程: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

这是为了将 collectionView 添加为页眉,但对于页脚同样有效。

private let tableView: UITableView = {
    let view = UITableView(frame: .zero, style: .plain)
    view.translatesAutoresizingMaskIntoConstraints = false
    view.estimatedRowHeight = 44
    view.rowHeight = UITableView.automaticDimension
    view.keyboardDismissMode = .interactive
    view.tableFooterView = //Your Custom View with CollectionView here
    return view
}()