Reputation: 1257
I am working on an app in which I will get data from server and I need to display images in scrollview with horizontal scrolling. I have done that previously showing single image with content of that image and horizontal scrolling. However I am not getting, how should we achieve showing 2 images in horizontal scroll. Any kind of help and suggestion would be really great help thanks.
Upvotes: 0
Views: 760
Reputation: 464
This solution will work for you.
int x = 0;
int imgW = self.galleryScrollView.frame.size.width/2;
int imgH = self.galleryScrollView.frame.size.height;
NSArray *imagesURL = @[@"https://s-media-cache-ak0.pinimg.com/236x/66/16/7f/66167f56c01bbcbdf32d68ceb414135a.jpg", @"https://s-media-cache-ak0.pinimg.com/236x/66/16/7f/66167f56c01bbcbdf32d68ceb414135a.jpg", @"https://s-media-cache-ak0.pinimg.com/236x/66/16/7f/66167f56c01bbcbdf32d68ceb414135a.jpg"];
NSInteger count = imagesURL.count;
for (int i=0; i<count; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, imgW, imgH)];
NSString *imgURL = imagesURL[i];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];
[self.galleryScrollView addSubview:imageView];
x = x + imgW;
}
self.galleryScrollView.contentSize = CGSizeMake(x, self.galleryScrollView.frame.size.height);
Upvotes: 1