UIScrollView won't scroll!

25,783

Solution 1

You need to set UIScrollView.contentSize to match the total scrollable size, which is your subview frame size in this case.

Solution 2

As mentioned in the accepted answer, you must set the UIScrollView's contentSize property.

This can be done in Interface Builder.

  1. Select the scroll view
  2. Select the 'identity inspect' in Utilities pane on the right
  3. Under 'User Defined Runtime Attributes' click the '+' button
  4. Set the 'Key Path' value to 'contentSize'
  5. Set the 'Type' value to 'Size'
  6. Set the 'Value' value to '{width, height}' (eg: '{320, 600}')

Build and run and your scroll view will scroll.

The content inset does not affect scrolling. See What's the UIScrollView contentInset property for?

Solution 3

To scroll, you have to make the scrollview's frame smaller than its content, the contained image or view.

Solution 4

This might be obvious to most, but I spent ages wondering why my UIScrollView wouldn't scroll so I'm posting what was stopping me in case it helps anyone else:

The UIScrollView has to be of the dimensions of the visible area in which you wish it to be presented and not the size of it's contents.

Ridiculous on my behalf I know, but just in case it helps someone.

Solution 5

I could solve the scrolling problem with the following answer:

https://stackoverflow.com/a/39945124/5056173

By me the trick was:

  1. You now need to set the height of the content UIView. You can then either specify the height of the content view (blech) or use the height of the controls contained within by making sure the bottom control is constrained to the bottom of the content view.

I have set the height and width of the view inside the scrollView with center vertical and horizontal alignment and that was the reason, why it did not work!

After deleting this constraints, I need to add equal width (scrollView and the view inside the scrollView) AND I set the height of the view inside the scrollView directly with the content. Which means: The last element in the view must have a bottom constraint to the view!!

Share:
25,783
Marty
Author by

Marty

I'm an iOS developer on the eBay app.

Updated on July 09, 2022

Comments

  • Marty
    Marty almost 2 years

    In IB I have my UIView. Then I have a sub-UIView with a UIScrollView as a sub view. Then the UIScrollView has a sub-UIImageView. The UIScrollView and UIImageView are the same size. They're much bigger than the UIView of which they are subviews. I assumed this would make scrolling work. It doesn't. Is some sort of code required for scroll views to work?