Sam
Sam

Reputation: 479

Swift:adding UIScroll view Programmatically does not scroll in view

I am trying to implement the UIScrollView programatically.

The main reason behind this is I have first added the views in my ViewController and finally i found that i forget to add the scroll view in my ViewController. Now i want to add it programiticaly but does not gets added in the view. I have searched everywhere and tried but did not helped yet.

   @IBOutlet weak var UIViewHeader: UIView!
@IBOutlet weak var btnRegisteredCourses: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var tableView: UITableView!

var scrollView: UIScrollView!

   override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = view.bounds.size
    scrollView.scrollEnabled = true
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
    view.addSubview(scrollView)
  }

Upvotes: 1

Views: 5467

Answers (6)

Alejandro S
Alejandro S

Reputation: 27

You might want to check Steve Hancock's answer in the similar question: UIScrollView not scrolling at all when added programmatically in Swift?

There is a lot of answers suggesting content size but this sometimes is not the solution. The problem lies in how the scroll view interacts with your views inside, and it is a good tendency to embed all your subviews inside a global UIView which is in turn embedded in the UIScrollView. This is a rough example on how you would set it up:

let scrollView = ScrollView() let contentView = UIView() (initialized above for simplicity)

    scrollView.isScrollEnabled = true
    scrollView.backgroundColor = .clear
    scrollView.topAnchor.constraint(equalTo: self.view).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    scrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true      
    scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 40).isActive = true

(you always want to make your scrollview longer than your view)

    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    coachView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -32).isActive = true

All of your subviews will go inside your contentView with their respective constraints.Technically you do not need the centerX anchors if you have a leading and a trailing but setting constraints for a scroll view can be a trial and error endeavor, especially if you have a subview with dynamic height. I have seen contentViews shift to the right for no apparent reason without the centerX anchor, and I have seen contentView lean to the left with a centerX anchor but without leading and trailing anchors

Upvotes: 0

Vinupriya Arivazhagan
Vinupriya Arivazhagan

Reputation: 564

Depends on which type of scrolling you want, you have to set the contentSize of scrollView. For example

For Horizontal Scrolling:

scrollView.contentSize = CGSize(width: UIViewHeader.frame.size.width + lblName.frame.size.width + tableView.frame.size.width + btnRegisteredCourses.frame.size.width , height: scrollView.frame.size.height)

For Vertical Scrolling:

scrollView.contentSize = CGSize(width: scrollView.frame.size.frame.size.width , height: UIViewHeader.frame.size.height + lblName.frame.size.height + tableView.frame.size.height + btnRegisteredCourses.frame.size.height)

Do like this:

override func viewWillAppear(_ animated: Bool) {
        scrollView.contentSize = CGSize(width: scrollView.frame.size.frame.size.width , height: UIViewHeader.frame.size.height + lblName.frame.size.height + tableView.frame.size.height + btnRegisteredCourses.frame.size.height)
    }

Upvotes: 1

Sandeep Kumar
Sandeep Kumar

Reputation: 328

Try This content will also scroll:

 @IBOutle var UIViewHeader: UIView!
@IBOutle var btnRegisteredCourses: UIButton!
@IBOutle var lblName: UILabel!
@IBOutle var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = CGSize(width:self.view.frame.size.width, height: 1000)
    scrollView.scrollEnabled = true

UIViewHeader.removeFromSuperview()
lblName.removeFromSuperview()
tableView.removeFromSuperview()
btnRegisteredCourses.removeFromSuperview()
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
    view.addSubview(scrollView)
  }

Upvotes: 1

An Kit
An Kit

Reputation: 328

    import UIKit
class ViewController: UIViewController {
@IBOutlet weak var UIViewHeader: UIView!
@IBOutlet weak var btnRegisteredCourses: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var tableView: UITableView!

var scrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = CGSize.init(width: self.view.frame.width, height: 1000)       //1000 change to the scollview's size.
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
   view.addSubview(scrollView)

}
 }

** Note:- But Above code reset all constraints **

Upvotes: 0

Sandeep Kumar
Sandeep Kumar

Reputation: 328

Increase scroll view content size and try it again.

and you have to remove UIViewHeader, lblName, tableView, btnRegisteredCourses from super view and after that add to scroll view.

Upvotes: 1

Lalit kumar
Lalit kumar

Reputation: 2197

override func viewDidLayoutSubviews()
{
    scrollView.delegate = self
    scrollView.contentSize = CGSize(width:self.view.frame.size.width, height: 1000)

}

Upvotes: 3

Related Questions