18

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?

Marty
  • 5,926
  • 9
  • 53
  • 91

11 Answers11

33

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

tia
  • 9,518
  • 1
  • 30
  • 44
  • 1
    I guess so. Never saw that in IB. – tia Nov 25 '10 at 04:57
  • Thank you very much. This really helped me. I wsa concidering redoing my interface if I hadn't found this – Andrew Jun 20 '11 at 04:26
  • @tia I think that "Content insets" in the IB is just for that. – Segev Mar 11 '13 at 14:48
  • @Sha Content insets are, well, the insets. It's not the same. – tia Mar 11 '13 at 15:40
  • @tia can you elaborate on the difference? I was facing the same issue as the op and changing the bottom inset to an higher value than 0 solved my problem. – Segev Mar 11 '13 at 15:49
  • 1
    @Sha First rule of using API is that you should use it the way suggested by API developer, or else it might go wrong now or later or in the case you did not imagine. In this case, content insets is for content padding so it should not be use to adjust the area of the content. Any different? Of course there is. Content insets are always fixed even it is zoomed, so it means if you are enabling zooming of the scrollview then your content will be clipped when zooming in and padded too much when zooming out. – tia Mar 12 '13 at 03:09
  • 19
    A better question might be, why can't the `UIScollView` work this out itself based upon its subviews? – aroth Aug 16 '13 at 04:07
  • Seriously @aroth. Coming from a web background this issue has given me many night terrors. – Kilmazing Apr 02 '19 at 14:10
15

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?

Community
  • 1
  • 1
Brenden
  • 7,708
  • 11
  • 61
  • 75
7

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

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • did that no luck. the scrollview's subview (uiimageview) is bigger than the scrollview. – Marty Nov 25 '10 at 01:07
  • Did you set the content size to match the size of the image? If not, you're just cropping the image... nothing to scroll is left. – hotpaw2 Nov 25 '10 at 01:53
  • I set the frame size in IB, is that what you mean? I didn't do anything with code though. – Marty Nov 25 '10 at 02:25
  • The content size is what scrolls inside the frame, AFAIK. You need to set that to the size of the image that's larger than the frame. – hotpaw2 Nov 25 '10 at 04:13
  • Amen to that. I was changing scroll view's width along with it's content size. Shouldn't be. – Ahmed Elashker Nov 03 '16 at 01:47
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.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • Just to clarify this a bit: the `UIScrollView` should be the size of what you want shown on the screen, and that scrollview's `contentSize` property should be set to the size of the area that will be scrolling (the contents). The scrollview won't scroll if its contentSize is smaller or equal to its frame size. – Olie Mar 14 '16 at 22:05
3

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!!

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
3

I placed all the content of my scrollview in IB. (buttons, labels, text fields, etc). The full size is 500 tall. I then resized it to 436 tall in IB.

Then in code, I put this is viewDidLoad:

optionsScrollView.contentSize = CGSizeMake(320,500);

So that leaves 64 pixels that I can scroll. It works perfectly.

I also placed "UIScrollViewDelegate" in the <> braces of @interface for my .h file and tied the delegate outlet of the scrollview to File's owner in IB.

RyeMAC3
  • 1,023
  • 1
  • 10
  • 17
2

The other important thing that I don't see mentioned here is that UIScrollView does not play nicely with AutoLayout. If it seems like you've done everything correctly, check if your ViewController has autolayout turned on and, if so, turn it off.

(Every time you scroll, the views are re-laid-out. Gak!)

So:

  • Make sure scrollview's contentSize is bigger than its frame.size
  • Make sure AutoLayout for the ViewController is turned off.
Olie
  • 24,597
  • 18
  • 99
  • 131
1

Make sure 3 things,

  • checking scrollView frame & contentView frame, u may find the answer

  • scrollView.isScrollEnabled = true

  • contentView of scrollView height didn't constraint with scroll view height

Bomi Chen
  • 81
  • 4
1

UIScrollView won't scroll!

  • reason: contentSize is same as (sub) view
    • should: contentSize is large than (sub) view
      • -> UIScrollView can scroll

how set UIScrollView contentSize?

two method:

in code

- (void)viewDidLoad {
    [super viewDidLoad];
    。。。
    //[(UIScrollView *)self.view setContentSize:CGSizeMake(375, 1000)];
    CGSize curScreenSize = UIScreen.mainScreen.bounds.size;
    CGFloat scrollWidth = curScreenSize.width;
    CGFloat scrollHeight = curScreenSize.height * 2;
    [(UIScrollView *)self.view setContentSize:CGSizeMake(scrollWidth, scrollHeight)];

in UI (Storyboard)

Storyboard-》Identity Inspector-》User Defined Runtime Attributes-》 add new attribute:

  • contentSize
    • Type:Size
    • Value:{375, 1000}

enter image description here

crifan
  • 12,947
  • 1
  • 71
  • 56
1

more, did you enable scrolling? look at the property

@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled
roberto.buratti
  • 2,487
  • 1
  • 16
  • 10
0

Scroll view works with this:
Frame
then
contentSize
views or objects etc...

If your frame is set to your content size then it won't scroll. So set your frame ( in IB right panel -> second last tab 'Size Inspector") to the length of your app ( in my case it is 367 as i have a navbar and a tab bar) then programatically set the contentSize to - yup you guessed it ... more than your frame so it can scroll.

Happy days!!

brummo
  • 9
  • 1