简体   繁体   中英

Swift: UIScrollView not scrolling vertically

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 .

http://nscookbook.com/2015/06/ios-programming-recipe-36-a-fixed-width-dynamic-height-scrollview-in-autolayout/

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.

在此输入图像描述

The way you are adding constraints is most likely the cause of your problem.

It appears that you are constraining the bounds of the content view to be the same as the scroll view. In this case, no scrolling can occur due to the constraints.

This is due to your including self.scrollView and self.contentView within tmpViewsDictionary .

In the tutorial that you linked, the dictionary is only being used for adding constraints to the views that are in the content view of the scroll view. It does not include the scroll view or the content view.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM