简体   繁体   中英

Xcode ScrollView doesn't scroll

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

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.

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.

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