ios - 如何在 Xcode/Swift 的 ScrollView 中将多个图像居中

标签 ios xcode swift

我试图在 ScrollView 中将 30 张图像水平居中。这样做的最佳方法是什么?谢谢。

enter image description here

编辑:感谢 David Cao,我能够以编程方式创建图像,但我仍然遇到图像不居中的问题。谢谢。 enter image description here

最佳答案

如果它们都是相同的图像,为什么不以编程方式执行此操作?只需创建一个 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/

相关文章:

iphone - "Apple Mach-O linker command failed with exit code 1"

ios - UINavigationItem 标题执行操作

ios - 如何创建半滑动ViewController

ios - 如何快速传递关联类型中的 Codable 对象

ios - 自定义 UITableViewCell 在 iPad 上宽度错误

ios - 在 ios 中使用后台

iOS Present modal view controller 在启动时没有闪光灯

xcode - 展开可选值(可选绑定(bind))时,不断收到意外发现 nil 的错误

iphone - 将 iphone 应用程序转换为 iPad、iOS?

swift - 无法实例化 View Controller Swift