0
votes

My VC is a vertical 2-part view controller, where the bottom part is a UIScrollView inside which I added a vertical UIStackView as it's supposed to vertically scroll.

All construction steps is done programatically as follows:

    // 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)
            ])

If I break Xcode and open up the view hierarchy debugger, I see it complains about content size: Scrollable content size is ambiguous for UIScrollView.

How it comes as I constraint the top and bottom stack view to be stuck to the scroll view?

1

1 Answers

1
votes

You may need to change this

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

to

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