卡在不滚动的UIScrollView中,并塞满了UIStackView

时间:2018-07-18 12:07:49

标签: swift uiscrollview uistackview

我的VC是一个垂直的两部分视图控制器,底部是一个UIScrollView,在其中添加了垂直的UIStackView,因为它应该垂直滚动。

所有构建步骤均以编程方式完成,如下所示:

    // Add upper part
    view.addSubview(upperView)
    upperView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        upperView.topAnchor     .constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        upperView.leadingAnchor .constraint(equalTo: view.leadingAnchor),
        upperView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        upperView.heightAnchor  .constraint(equalTo: view.heightAnchor, multiplier: 0.3)
    ])


    // Add down part

    view.addSubview(scrollView)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        scrollView.topAnchor     .constraint(equalTo: upperView.bottomAnchor,  constant:  40),
        scrollView.leadingAnchor .constraint(equalTo: view.leadingAnchor,      constant:  20),
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor,     constant: -20),
        scrollView.bottomAnchor  .constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    ])

    // Vertical stackview: basically a vertical collection of strips
    scrollView.addSubview(stackView)
            stackView.spacing = 5
            stackView.distribution = .fill
            stackView.alignment = .leading
            stackView.axis = .vertical

            stackView.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                stackView.topAnchor   .constraint(equalTo: scrollView.topAnchor),
                stackView.leftAnchor  .constraint(equalTo: scrollView.leftAnchor),
                stackView.widthAnchor .constraint(equalTo: scrollView.widthAnchor),
                stackView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor)
            ])

如果我中断Xcode并打开视图层次调试器,则会看到它抱怨内容大小:UIScrollView的可滚动内容大小不明确。

当我限制顶部和底部堆栈视图以卡在滚动视图中时,会怎么样?

1 个答案:

答案 0 :(得分:1)

您可能需要更改此内容

stackView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor)

stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)