1
votes

After appending last view with elements (which is inside scroll view) the scroll view height is not changing, so I cannot scroll to bottom of content

Steps which I done:

  1. Added UIScrollView view, set top, right, bottom and left align to 0
  2. Added UIView, set top, right, bottom and left align to 0
  3. From View inside scroll view: set equal width and equal height to the main view (the view that holds the scrollview)
  4. For First View added constraints: Top Constraint to container view: 20. Left Constraint: 8. Right
    Constraint: 8. Bottom Constraint: Standard. Height: 320
  5. For Second View added constraints: Top Constraint to container view: Standard. Left Constraint: 8. Right Constraint: 8. Bottom Constraint: Standard

So it looks like:

enter image description here

After this I add manually labels to last View:

class SecondViewController: UIViewController {

    @IBOutlet weak var testView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var yPos = 20
        for i in 0...20 {
            let testLabel = UILabel(frame: CGRect(x: 0, y: yPos, width: 200, height: 20))
            testLabel.text = "test \(i)"
            testLabel.font = UIFont(name: "Arial", size: 11)

            testView.addSubview(testLabel)
            yPos += 20
        }
    }
}

enter image description here

As you can see I can only see labels 1-9, rest are not visible because scrolling to bottom is not possible

It is problem because I mixing auto layout with programmatically added labels to View? How I can resolve this issue?

2
check the scrollView.contentSizeReinier Melian

2 Answers

1
votes

Your last question/point is correct, you should use auto layout if you're using auto-layout in the Storyboard.

Luckily, UIStackView works great with auto layout, so if you change the yellow view to be a UIStackView and make an outlet to that in code, you can just add views:

stackView.addArrangedSubview(sensorLabel)

A short explanation of why it won't let you scroll without updating auto-layout:

Your yellow view is pinned to the labels that are inside it, and the bottom of the yellow view is also pinned to the bottom of the scroll view in the Storyboard.

Unless you increase the size of the yellow view (by changing which label the bottom of the yellow view is pinned to), the scroll view content size won't change either because it's pinned to the bottom of the yellow view. A stack view will do most of this work for you, so you just need to pin the bottom of the stack view to the bottom of the scroll view in the Storyboard.

0
votes

The problem is that you set height constraint of it fixed read step 3 try to remove it and hook last label bottom constraint to the content view of the scrollview