UIScrollView中的嵌套UIStackView子级无法拉伸填充

问题描述 投票:0回答:1

我有一个UIStackView

我添加了一些意见。

最后一个视图应位于屏幕底部。为此,我添加了一个用作间隔的视图,并在第一个和最后一个视图上设置了高度,以使中间视图延伸以填充空间。

这有效。

    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($0) }

enter image description here

但是,我的屏幕尺寸可能小于视图的尺寸。

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


        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($0) }

enter image description here

我希望在这种情况下,我的UIStackView仍然可以填充视图,并且只有在子视图导致其高度增长到视图的可见范围之外时才可以滚动。

swift uiscrollview autolayout uistackview
1个回答
0
投票

[将UITableView与UICollectionView一起用作页脚。

您可以在此处找到教程: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
}()
© www.soinside.com 2019 - 2024. All rights reserved.