UIScrollView not scrolling although contentSize is smaller than UIImageView

19,970

Solution 1

That's because that your size of bounds of scroll view is larger than your content size. The content size need to be the actual size of image view, and the visible size is set by frame / bounds. I guess you want to scroll a image in a {200, 200} sized rect? Try this:

        UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // this is your visible rect
        [imgScrollView setScrollEnabled:YES];
        [imgScrollView setClipsToBounds:YES];
        [imgScrollView addSubview:imgView];
        [imgScrollView setBackgroundColor:[UIColor yellowColor]];

        [imgScrollView setContentSize:imgFrame.size]; // this is your image view size

Solution 2

the janky way to do this is to always set the contentSize to be larger than the view size. But there's a property in Swift that allows a scrollview to always "scroll" regardless of the content size:

scrollView.alwaysBounceVertical = true

There's one for horizontal scrolling as well

Solution 3

I know this is old, but I want to point out that when you create a UIScrollView in code the most important thing to set is:

scrollView.scrollEnabled = YES;

If you don't set that, your contentSize is irrelevant.

Share:
19,970
adit
Author by

adit

Updated on June 09, 2022

Comments

  • adit
    adit almost 2 years

    So I have a UIImageView as a subview of UIScrollView, I've set the contentSize to be smaller than the UIImageView width and height, however it doesn't allow me to scroll.. why is this? Here's some code:

    UIImage * image = [UIImage imageWithData:data];
                    UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
                    [imgView setUserInteractionEnabled:YES];
                    //[imgView setContentMode:UIViewContentModeScaleAspectFill];
                    [imgView setBackgroundColor:[UIColor clearColor]];
                    [imgView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
                    [imgView setFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
    
                    CGRect imgFrame;
                    imgFrame.size.width = originalImageSize.width;
                    imgFrame.size.height = originalImageSize.height;
                    imgFrame.origin.x = imageOriginPoint.x;
                    imgFrame.origin.y = imageOriginPoint.y;
    
                    UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imgFrame];
                    [imgScrollView setScrollEnabled:YES];
                    [imgScrollView setClipsToBounds:YES];
                    [imgScrollView addSubview:imgView];
                    [imgScrollView setBackgroundColor:[UIColor clearColor]];
    
    
      [imgScrollView setFrame:imgFrame];
                    [imgScrollView setContentSize:CGSizeMake(200, 200)];
    
    SCROLL VIEW CONTENT SIZE WIDTH IS 200.000000 AND HEIGHT CONTENT SIZE IS 200.000000
    UIIMAGE VIEW WIDTH IS 2240.225830 AND HEIGHT IS 2240.225830
    SCROLL VIEW FRAME WIDTH IS 768.000000 AND SCROLL VIEW FRAME HEIGHT IS 768.000061
    

    Any idea?

  • adit
    adit over 12 years
    so the frame size has to be smaller than the contentSize? if the frame size is the same as the contentSize then it won't scroll?
  • Linghua Zhang
    Linghua Zhang over 12 years
    By default, YES. But if you set alwaysBounceVertical and alwaysBounceHorizontal to YES (both default NO), you can drag your content view even the contentSize is smaller than frame size
  • sam-w
    sam-w about 11 years
    This is much better suited to being a comment, rather than an answer. In any case it is misleading: as stated in the docs, the default value for scrollEnabled is YES. You do not need to set scrollEnabled to YES in code, unless you have previously set it to NO.
  • Alyoshak
    Alyoshak almost 11 years
    I disagree. It works as an answer also. (And it just solved my problem. Thanks Chris.) A lot of people scroll down looking at the answers, and don't read all (or any) of the comments unless the answer leads them to. Sometimes you're working with code written by another and you may not be familiar with it all.
  • Chris
    Chris almost 11 years
    I had to set mine to YES to get it to scroll.
  • Raj Pawan Gumdal
    Raj Pawan Gumdal over 9 years
    It was driving me nuts coz the scrollview was not scrolling when contentSize was less than the bounds' size. In my case, I want to reduce the content size of scrollview when keyboard comes up, I was amazed to see that reducing content size does not make the scroll view to scroll. Guess, I have to change the frame of the scroll view instead.