views:

249

answers:

1

I'm loading a UIScrollView with UIImageViews. imageViews below is an NSMutableDictionary of UIImageViews. The scrollview is an IBOutlet with scrolling and paging enabled. The loop adds two images. However, I only ever see the first image in the scrollview and am not able to scroll. Is there something else I need to enable for scrolling to work?

for(NSString *s in [imageViews allKeys]){
    UIImageView *myImageView = [imageViews valueForKey:s];
    myImageView.frame = CGRectMake(0, 0, 120, 90);
    myImageView.contentMode = UIViewContentModeScaleAspectFit;
    scrollView.contentSize = CGSizeMake(120, 90);
    [scrollView addSubview:myImageView];
}

[scrollView layoutSubviews];
+2  A: 

I'm guessing your scrollView in IB is about 120x90? Then you'll need to set your contentSize to be at least 240x90 in case of 2 images, and (n * 120)x90 for n the amount of imageViews, assuming you want to be able to scroll/page horizontally. This is because the contentSize indicates the entire "virtual" area of your images.

Also: you're currently placing each imageView at the same coordinates:

myImageView.frame = CGRectMake(0, 0, 120, 90)

This is why you only see one image, they overlap. You'll need to place each consecutive imageView at least 120 to the right of the previous.

mkrause
I've commented out [scrollView layoutSubviews] and all works fine. Is that needed for anything?
4thSpace
No, I don't think you need it here. I've only seen layoutSubviews used in cases like resizing animations.
mkrause