UIScrollView的大小和图像大小

时间:2015-04-21 19:08:05

标签: ios iphone xcode uiscrollview

我创建了这个动态加载图片的滚动视图。但是我无法将图像覆盖在屏幕上(应该适应iPhone / iPad)一对一。并且图像的高度也已损坏(参见附图)

我做错了什么?

图像

enter image description here

加载图片的来源:

 for (int i=0; i < numberOfImages; i++) {

        // create image
        NSString *imageName = [NSString stringWithFormat: imageArray[i], i+1];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // put image on correct position
        CGRect rect = imageView.frame;
        rect.origin.x = currentX + self.scrollView.frame.size.width/2;
        imageView.frame = rect;

        // update currentX
        currentX += imageView.frame.size.width + self.scrollView.frame.size.width;

        [self.scrollView addSubview:imageView];
        [imageView release];
    }

    self.scrollView.contentSize = CGSizeMake(currentX, self.view.frame.size.height);

    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [self.view addSubview:scrollView];
    [self.scrollView release];

2 个答案:

答案 0 :(得分:2)

这应该适合你:

for (int i = 0; i < numberOfImages; i++) {

    // create image
    NSString *imageName = [NSString stringWithFormat: imageArray[i], i+1];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeCenter;

    // put image on correct position
    CGRect rect = self.scrollView.bounds;
    rect.origin.x = currentX;
    imageView.frame = rect;

    // update currentX
    currentX += self.scrollView.frame.size.width;

    [self.scrollView addSubview:imageView];
    [imageView release];
}

self.scrollView.contentSize = CGSizeMake(currentX, self.scrollView.frame.size.height);

您还可以将scrollView分页属性设置为yes,因此它将逐页滚动。

答案 1 :(得分:1)

您应该使用具有水平滚动的UICollectionView而不是scrollView,并且您可以从集合视图实现委托以提供具有所需尺寸的单元格。实施将变得非常简单和直接。