ScrollView won't scroll

Hello.

I have successfully created a scrollview with several buttons that stack on top of one another. But the View won't scroll. What am i doing wrong here?

Code Block swift
scrollView = UIScrollView.init(frame: CGRect.zero)
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
var leadingAnchor = self.scrollView!.topAnchor
for i in 0..<20{
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.topAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
t_button.heightAnchor.constraint(equalToConstant: 50.0),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.bottomAnchor
t_button.setTitle("Button \(i)", for: .normal)
t_button.addTarget(self, action: #selector(scrollViewButtonAction(_:)), for: .touchUpInside)
}
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.titleHeader.bottomAnchor, constant: 10.0),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
])


Answered by OOPer in 651104022
Adding constraints to UIScrollView is sort of mystery for me, so I am not sure if this works for your project,
but please try adding this line after your line 24. (Just after the for loop is finished.)
Code Block
        leadingAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true



By the way, UIKit is the right tag for questions of UIScrollView, not SwiftUI.
Accepted Answer
Adding constraints to UIScrollView is sort of mystery for me, so I am not sure if this works for your project,
but please try adding this line after your line 24. (Just after the for loop is finished.)
Code Block
        leadingAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true



By the way, UIKit is the right tag for questions of UIScrollView, not SwiftUI.
ScrollView constraints is really a mess and often a headache.

I wrote for me a detailed procedure to remember. That's for UIKit.

Building a ScrollView with its constraints

ScrollView is composed of a scrollview, a content view and objects in content view to be scrolled.

One need to have the concept clearly in mind:
  • it is the contentView which contains all of what will be displayed

  • the scroll is a window that passes over the content to see part of it

  • and yet contentView is INSIDE ScrollView, which may be misleading

And so
  • it will scroll in one direction only if the content is bigger than the scrollView in that direction

  • the contentView should be set to include « exactly » all that should be showable. If too large, it will scroll beyond the content and we will see an empty area

Create a ScrollView

Define its dimensions by constraints relating to the Safe area. Such as in this example
  • leading = 0 maybe non zero if you scroll an horizontal part of the screen

  • trailing = 0

  • top = maybe non zero if you scroll a part of the screen

  • bottom = 35 (here to leave room for some buttons below)

you can keep the content layout and frame layout ON

Create a UIView (this is the content View, named View here)

Place it inside the scrollView (appears inside Scroll in the object hierarchy)
Define the constraints with respect to the scrollView (Superview)
  • leading = 0

  • trailing = 0

  • equalWidth with Scroll: we don't want to scroll horizontally

  • top = 0

  • bottom = 0

Above all, do NOT make equal height with Scroll: there would be no more vertical scroll

Define the dimension constraints of the ContentView to correspond to the complete content above which we will scroll (here I had 4 items and 3 switches)
  • height = 262

  • width has been set equal to the width of the scrollView // Note: this is necessary to silence red warnings from IB

Place objects in the ContentView
for each
  • Define the constraints to the ContentView.


Almost, but thanks A BUNCH for pointing me in the right direction!

This is the working solution:

Code Block swift
 scrollView = UIScrollView.init(frame: CGRect.zero)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.isUserInteractionEnabled = true
    scrollView.isScrollEnabled = true
    self.view.addSubview(scrollView)
    NSLayoutConstraint.activate([
         scrollView.topAnchor.constraint(equalTo: self.titleHeader.bottomAnchor, constant: 10.0),
         scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
         scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
         scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
       ])
     
     
    var leadingAnchor = self.scrollView!.topAnchor
    for i in 0..<20{
      let t_button = UIButton.init(frame: CGRect.zero)
      t_button.translatesAutoresizingMaskIntoConstraints = false
      t_button.backgroundColor = UIColor.blue
      scrollView.addSubview(t_button)
      NSLayoutConstraint.activate([
        t_button.topAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
        t_button.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
        t_button.heightAnchor.constraint(equalToConstant: 50.0),
        t_button.widthAnchor.constraint(equalToConstant: 75.0)
      ])
       
      leadingAnchor = t_button.bottomAnchor
       
      t_button.setTitle("Button \(i)", for: .normal)
      t_button.addTarget(self, action: #selector(scrollViewButtonAction(_:)), for: .touchUpInside)
       
    }
    leadingAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true


ScrollView won't scroll
 
 
Q