我试图在 ScrollView 中将 30 张图像水平居中。这样做的最佳方法是什么?谢谢。
编辑:感谢 David Cao,我能够以编程方式创建图像,但我仍然遇到图像不居中的问题。谢谢。
最佳答案
如果它们都是相同的图像,为什么不以编程方式执行此操作?只需创建一个 for 循环并遍历边界即可。
NSInteger numWidth = 3;
NSInteger numHeight = 10;
CGFloat border = 8;
CGFloat width = (self.scrollView.frame.size.width - (numWidth + 1) * border)/3;
for (NSInteger i = 0; i < numWidth; ++i) {
for (NSInteger j = 0; j < numHeight; ++j) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageNameHere"]];
imageView.frame = CGRectMake(border + width*i, border + width*j, width, width);
[self.scrollView addSubview:imageView];
}
}
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, (border + width)*numHeight + border];
关于ios - 如何在 Xcode/Swift 的 ScrollView 中将多个图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487377/