问题
I'm trying to implement scrollview using latest xcode.
I have content that reaches 'out of bounds' and when I run the simulator, I'm unable to scroll down to see the rest of my content.
My hierarchy / constraints are as follows..
Are there constraints you need to have on the subviews within the 'contentViiew' ? I don't have additional code in my view controller that manipulates the scrollview.
using Xcode -v 11 & Swift -v 5 <- if this makes a difference
回答1:
Please clear all constraints on contentView and proceed as follows. You need the following constraints for a vertically scrolling scrollView:
1) Constraint your ContentView to have zero distance from all over sides of scrollView.
2) Equal height and width constraint to scrollView. The height constraint to have lower priority (say 400).
The only constraint for subviews is that they all have constraints set relative to view just above them. Also, the bottom subview should have a bottom constraint for scrollView to scroll.
What I mean by this is that all UIViews should have relative vertical constraints. for example, if going from top to bottom, you have three views - A->B->C, then following constraints are required - 1) top to A 2) A to B 3) B to C and most importantly 4) C to bottom. If you get an error in interface builder, you can use ">=" instead of "=" constraint for the #4. I hope that helps.
回答2:
Add a dummy view(UIView) inside a scroll view with zero width. Set constraint of this view like this.
My dummyView Top constraint = scroll view top constraint
My dummyView bottom constraint = scrollView Bottom Constraint
My dummyView leading Constraint = scrollView Leading Constraint
My dummyView Height constraint = give height according to your need (eg 1500)
My dummyView width constraint = 0
Now whenever you want to change height of scroll view just change height of dummy view.
来源:https://stackoverflow.com/questions/59441927/xcode-scrollview-doesnt-scroll