Uiscrollview Controller Not Scrolling Fully

UIScrollView Controller not scrolling fully

Edit:

I have created a little example on github for you to look at, here. The project illustrates the answer below and uses the techniques I describe and nothing else.

Original Answer:

couple of things I would advise here.

  1. First, I know you've been trying for a while but remove all the current constraints (painful I know but). Do this for clarity as ....

  2. The view should be the size of the scene, it looks like you want the scrollview to be the full screen so that too needs to be the size of the scene.

e.g. if you are designing at 6Plus by default the scene size is 414x736 so the view and the scrollview it contains should also be 414x736.


  1. Only the content view needs to be the size of the real content you wish to show. Let's say for arguments sake that the content is 414x1000.

Now the constraints for the scrollview are simple. It needs zero spacing to all it's edges.

Sample Image

You can add the content view to the scrollview in a couple of ways. The way I try to do this varies from project to project and depends mostly on how complex the scene is. If it's a really busy scene I keep the content view outside of the scrollview in interface builder so that I can work on it easily and visualize the whole of the view. Then I add the content view to the scrollview in code.

If its a simpler view You can add it inside the scrollview in interface builder. Ultimately whichever way you do it, you can lose visibility of the content view in interface builder because the contentview is larger than the scrollview and the content gets obscured. So play about and find a good way for you.


  1. Define the content view and all it's subviews. The content view needs to be taller than the scrollview otherwise it wont scroll. All of the content view's subviews need to have defined heights from top to bottom and widths from left to right. In your case the scrollview is scrolling vertically not horizontally so all the widths need to add up to the width of the scroll view BUT the heights need to add up to the full height of the content view.

Note: if you do this proportially your life will be easier later. If you do all this with fixed heights the storyboard will break on different device sizes.


  1. Now the "tricky bit" and it's a bit counter intuitive. You need to pin the content view to the scrollview, remember the height of the content view is taller than the scrollview. In all other circumstances in Interface Builder pinning a view to a superview (0 padding) will adjust the height (or width) accordingly. For the relationship between a scrollview and it's content view this doesn't happen.

First pin the contentview

Sample Image

Notice the -400? Remember the content view is taller than the scrollview and we will change this immediately.

Select the bottom constraint (-400) that we have just created:

Sample Image

Select the drop down arrow next to the constant value:

Sample Image

Select Standard Value and type in 0 for the constant.

You should now have a storyboard with no broken constraints and if you build and run you should get a scrollview as desired.

UIScrollView is not scrolling despite setting delegate

Problem is with these two lines of code

self.scrollView = UIScrollView(frame: CGRect(x: 0, y: displayHeight - displayHeight / 3, width: displayWidth, height: displayHeight))
self.scrollView.contentSize = CGSize(width: displayWidth, height: displayHeight)

Either the scrollView.frame.size should be smaller or the contentSize should be larger. If both are same, the scrollView has no need to scroll.

If only the heights of frame and content size are different, you'll get vertical scroll. if it is the widths, you get horizontal scroll.

UIScrollView not scrolling in Swift

You need to set the frame of your UIScrollView so that it is less than the contentSize. Otherwise, it won't scroll.

Also, I would recommend that you add the following to your viewDidLoad method:

scroller.contentSize = CGSize(width: 400, height: 2300)

Swift: UIScrollView not scrolling vertically

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

My ScrollView is not scrolling vertically

You need to add a UIView inside your scrollview and then add that view top/bottom/leading/trailing 0 to scrollview then add your image view inside view and add image view bottom constraint to that view, So your scroll view will work properly.

Please refer below image for more details.

Sample Image

UIScrollView not scrolling regardless of large contentSize

Turning Auto Layout off works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.

UIScrollView works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit (emphasis mine, at third bullet point):

Here are some notes regarding Auto Layout support for UIScrollView:

  • In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to
    the left edge of its superview, you’re really pinning it to the
    minimum x-value of the superview’s bounds. Changing the bounds origin
    of the superview does not change the position of the view.
  • The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom,
    and right edges within a scroll view now mean the edges of its content
    view.
  • The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the
    scroll view. (This should not be confused with the
    intrinsicContentSize method used for Auto Layout.) To size the scroll
    view’s frame with Auto Layout, constraints must either be explicit
    regarding the width and height of the scroll view, or the edges of the
    scroll view must be tied to views outside of its subtree.
  • Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints
    between the view and a view outside the scroll view’s subtree, such as
    the scroll view’s superview.

Apple then goes on to show example of how to correctly use UIScrollView with Auto Layout.

As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:

Sample Image

Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.

UIScrollView Not Scrolling?

  1. viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.

  2. Are you using autolayout? If so, check out the following answer (and forget 1.):
    UIScrollView doesn't use autolayout constraints.



Related Topics



Leave a reply



Submit