1

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)
  }
4
  • UIScrollView can scroll if the contentSize is greater than its frame size. So you just need to set contentSize with height greater than scroll's frame height (in case vertical scrolling). Commented Jan 10, 2017 at 6:54
  • oh thank you..i was unaware about this.. :) :) Commented Jan 10, 2017 at 6:55
  • ok i set the content size greater to the height. but only scroll bar scrolls with out scrolling the view? Commented Jan 10, 2017 at 7:02
  • Because of the subViews you added was created before and they are related with self.view, they are the subViews of self.view. Try the answer of @Sandeep Kumar. If it cannot fix, try to create all of subViews programmatically. Commented Jan 10, 2017 at 7:07

6 Answers 6

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

i can just find the scroll bar and bar only scrolls but does not scroll the view
1

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.

1 Comment

do you mean contentSize?
1

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)
  }

5 Comments

can you explain why we are removing from superView?
it is working?. Because in storyboard these views are added to self.view not on scrollview.
i am trying to implement,it's taking time
it is still problem. only scroll bar moves without views
Add all content to view in storyboard and then add view to scrollview.
1

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)
    }

3 Comments

Views are not being scrolled
@Samjhana, set scrollView content size in viewWillAppear
sorry i did no get what you means
0
    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 **

1 Comment

Views are not being scrolled
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.