I have looked at this post : scrollView not scrolling swift
and I see that the accepted answer suggested the person to add bottom constraints to the UIScrollViews
subview, contentView
. I have the following view hierarchy and constraints on each view:
UIView -> UIScrollView(scrollView) -> UIView(contentView)
//in the View Controller
self.view.addSubview(coverImage)
scrollView.addSubview(contentView)
view.addSubview(scrollView)
here are the NSLayoutConstraint
s I add to scrollView
and contentView
let tmpViewsDictionary = ["scrollView":self.scrollView, "contentView": self.contentView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",options: [], metrics: nil,views: tmpViewsDictionary))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",options: [], metrics: nil, views: tmpViewsDictionary))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[contentView]-0-|", options: [], metrics: nil, views: tmpViewsDictionary))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[contentView]-0-|", options: [], metrics: nil, views: tmpViewsDictionary))
self.view.addConstraints([NSLayoutConstraint(item :self.contentView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width , multiplier: 1, constant: 0)])
I am following the general structure of this tutorial, which fixes the width and has flexible height of the scrollView
.
When I add content which exceeds the actual parent view Size, I can't scroll down to see the rest of it.
It seems that how they make the height dynamic and the width fixed is to simply constraint the child view of UIScrollView
to have an equal width as the parent of UIScrollView
. This I do successfully, but the scrolling is still not happening. Thank you for your help!
Here is the full ViewController
for this view:
https://gist.github.com/ebbnormal/90c35c435320e0ec1307e95f575119bf
UPDATE
I used Daniels great advice of removing anchoring the bottom of contentView
to the scrollView
bottom.
So after debugging the view hierarchy, I see that contentView
is being set to less than its parent view height, and this seems to stem from an Auto Layout constraint setting the self.bottom
of contentView
to label.bottom
of a UILabel
which is a child of contentView
. I never set this constraint and I don't know how to get rid of it.
Here is what that looks like in the ViewHierarchy, where the highlighted View
is the contentView
which you see is cut off.
EUREKA. I was foolishly setting the vertical constraint of a child of contentView
and pinning it to the bottom of contentView
hence why contentView
was cut short and wasn't scrolling.
"V:|-300-[wikiTitle]|"
The last "|"
cut off the child of my UIScrollView
short.
Finally I needed to pin the last item of contentView
to be a specific spacing away from the bottom of contentView