Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView won't scroll!

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?

like image 994
Marty Avatar asked Nov 25 '10 00:11

Marty


4 Answers

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

like image 94
tia Avatar answered Nov 08 '22 18:11

tia


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?

like image 28
Brenden Avatar answered Nov 08 '22 17:11

Brenden


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

like image 6
hotpaw2 Avatar answered Nov 08 '22 18:11

hotpaw2


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.

like image 4
Adam Waite Avatar answered Nov 08 '22 17:11

Adam Waite