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), ])
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.