Unable to set the width of scroll View iOS Swift

Viewed 2723

I am unable to set the width of the scrollView to the width of the screen. Could someone please guide where I am going wrong ?

enter image description here

Here is how I have enabled a scroll view

// SCROLL VIEW
var scrollView = UIScrollView()
var contentView = UIView()


override func viewDidLoad() {
    super.viewDidLoad()

    screenHeight    = screenSize.height
    screenWidth     = screenSize.width

    // SCROLL VIEW
    scrollView = UIScrollView(frame: view.bounds)

   // Here I am adding all my labels and textview fields to content view
   contentView.addSubview(problemDescriptionStackView)
   ....

    // Adding content view to the scrollView
    scrollView.addSubview(contentView)

    // Pinning the contentView to the scroll view
    pinView(contentView, to: scrollView)

    self.view.addSubview(scrollView)

    // Here I am Setting the constraints for all the items in the contentView
    ....
}

// The below functions are used to pin one view to another view

   public func pinView(_ view: UIView, to scrollView: UIScrollView) {
        view.translatesAutoresizingMaskIntoConstraints = false
        view.pin(to: scrollView)
    }

    public func pin(to view: UIView) {
        NSLayoutConstraint.activate([
            leadingAnchor.constraint(equalTo: view.leadingAnchor),
            trailingAnchor.constraint(equalTo: view.trailingAnchor),
            topAnchor.constraint(equalTo: view.topAnchor),
            bottomAnchor.constraint(equalTo: view.bottomAnchor)
            ])
    }
9 Answers
Related