UIScrollView not scrolling

Discussion in 'iOS Programming' started by larswik, Feb 8, 2012.

  1. larswik macrumors 68000

    Sep 8, 2006
    I have a couple images that I am adding to a UIScrollView that are 320w * 180h, some nice letter boxed photos. The photos get added and everything looks good but it won't scroll.

    in my viewDidLoad I have this code

    [self.view addSubview:[self scrollViewPhotos]];
    To test this so far I added a couple of images to an NSMutableArray. I can see that it returns the images and I can offset the images horizontally to see that they are both there. For some reason it won't scroll.

    -(UIScrollView *)scrollViewPhotos{
        int numOfImages, imageYsize;
        numOfImages = 2;
        imageYsize = 320;
        NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
        UIImage *image1= [UIImage imageNamed:@"food_one_small.jpg"];
        UIImage *image2= [UIImage imageNamed:@"food_two_small.jpg"];
        [imagesArray addObject:image1];
        [imagesArray addObject:image2];
        UIScrollView *clientPhotos = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, 640, 180)];
        clientPhotos.pagingEnabled = YES;
        for (int i = 0; i < numOfImages; i++) {
            CGFloat imageStartPoint = imageYsize * i;
            UIImageView *itemToAdd = [[UIImageView alloc] initWithFrame:CGRectMake(imageStartPoint, 0, 320, 180)];
            itemToAdd.image = [imagesArray objectAtIndex:i];
            [clientPhotos addSubview: itemToAdd];
        return clientPhotos;
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
  3. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    You need to set the contentSize of the UIScrollview to a bigger size then your picture, so it will zoom/scroll.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    I see, that has not been set then but I have a question about that. I am guessing that they contentSize would be the size of the photo that I am importing then?

    If that is the case that size needs to be a variable. My images are 320 x 180 and I might have any where from 1 to 5 photos so it could be up to 1600 X 180. I am not sure where I set the contentSize. You can see the photo that I included that I am wanting to have the image in a frame and they they user swipes it to the next photo.

    I need to find out where I my method I set up the content size.


    Attached Files:

  5. larswik thread starter macrumors 68000

    Sep 8, 2006
    Got it! I I found my 2 mistakes. After posting I thought it through and got it.

    First I added the and it did not work.
    clientPhotos.contentSize = CGSizeMake(640, 180);
    Then I thought I also made my UIScrollView the same size as my content. I made the width of the scrollView only the size of the screen with 320 instead of 640.

    It works perfect now!

    Thanks for the push in the right direction!

Share This Page