UIScrollView not scrolling

Discussion in 'iPhone/iPad Programming' started by larswik, Feb 8, 2012.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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

    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.

    Code:
    -(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. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Did you set contentSize?
     
  3. macrumors 65816

    jnoxx

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

    Joined:
    Sep 8, 2006
    #4
    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.

    Thanks!
     

    Attached Files:

  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    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.
    Code:
    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