简体   繁体   中英

Images dynamically in iOS scrollview

I'm developing an iOS App and i'm trying to put images dynamically in a scrollview.

I declared the scrollview and i put images inside it. The images are shown in the right place, but the scrollview doesn't scroll.

This is my code:

imagesScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight];

for (int i =0; i<dim; i++) {  //dim is the number of the images to show

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:previewImage_URL]];
previewImages = [UIImage imageWithData:data];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,10+(200*i),screenWidth,200)];

[imageView setImage:previewImages];

[imagesScrollView addSubview:imageView];

}

How can i do this?

您必须使用

[imageScrollView setContentSize:CGSizeMake(100,200)];

You need to adjust the scrollview's content size to a size larger than the scrollview, so the scrollview has something to scroll to. In this case the minimal content size needed to display all of the images can be set by using:

[imageScrollView setContentSize:CGSizeMake(screenWidth, 10+(200*(dim - 1)) + 200)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM