gpt4 book ai didi

ios - UIScrollView 的大小和图像的大小

转载 作者:行者123 更新时间:2023-12-01 18:52:54 24 4
gpt4 key购买 nike

我创建了这个动态加载图像的 ScrollView 。但是我无法让图像一张一张地覆盖屏幕(应该适应 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];

最佳答案

这应该适合你:

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,因此它将逐页滚动。

关于ios - UIScrollView 的大小和图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29781044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com