0

UIScrollView is not scrolling the subviews but it does show a scroll-bar.

Here is what I am trying to do

class SetupViewController: UIViewController {

    let scrollView = UIScrollView()
    let pageLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupViews()
        self.setupConstraints()
        self.setText()
    }

    func setupViews() {
        self.scrollView.backgroundColor = .red
        self.scrollView.translatesAutoresizingMaskIntoConstraints = false
        // Page Label
        self.pageLabel.font = UIFontLocalized(englishFontSize: 22, arabicFontSize: 22)
        self.pageLabel.textAlignment = .center
        self.pageLabel.translatesAutoresizingMaskIntoConstraints = false
    }

    func setupConstraints() {
        // Add To Sub Views
        self.view.addSubview(self.pageLabel)
        self.view.addSubview(self.scrollView)
        // Page Label
        NSLayoutConstraint(item: self.pageLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 30.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: -40.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36.0).isActive = true
        // Scroll View
        NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.line1, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: -100.0).isActive = true
        self.scrollView.contentSize.height = 2000

        // NSLayoutConstraint for rest of elements are removed in this example code.
    }
}

I can confirm UIScrollView is added in the view because I checked by giving background red color to scrollview and it does show the background red color in correct position. My issue is the subviews does not move only the scroll bar is moving.

What could be the possible issue here?

Note: NSLayoutConstraint exist for all UIKit elements, I have not added it in the code.

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • 1
    I have only quickly skimmed through the code, but I think the issue might be because you are constraining the content of the scroll view (subviews) to topLayoutGuide, which is outside the scroll view. Instead try to constraint subviews to superview. – Stanislav Goryachev Jan 11 '18 at 13:57
  • 1
    https://stackoverflow.com/a/48219419/9086770 I would suggest you read through that to not only get this scroll view working but to understand how to properly use constraints with `UIScrollView`. However, we can't answer your question at the moment because you haven't shown the constraints of the scroll view's subviews, which is undoubtably causing your problem. You haven't properly anchored the constraints of the scroll view's subviews to the bounds of the scroll view itself and, therefore, the scroll view will never scroll. – trndjc Jan 12 '18 at 17:27
  • 1
    I got this issue sorted out. Thanks @Stanislav Goryachev for the pointer. the issue was, In some constraint I was referring to controller's view as a super view whereas I had to use ScrollView as super view. changing the superview in constraint solved my issue. – Ibrahim Azhar Armar Jan 13 '18 at 06:29

1 Answers1

0

I got this issue sorted our. As pointed out in the comment, the issue was related to superview in constraint which was set to self.view changing it to self.scrollView solved the issue.

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207