我正在尝试在滚动视图中水平居中30个图像。这样做的最佳方法是什么?感谢。
编辑:以编程方式我能够创建图像,感谢David Cao,但我仍然没有居中的问题。谢谢。
答案 0 :(得分:2)
如果他们都是相同的图片,为什么不以编程方式执行此操作?只需创建一个for循环并遍历边界。
Post
答案 1 :(得分:1)
我能够解决这个问题。我使用
修改了David Cao的代码以考虑各种屏幕宽度let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width`
我将scrollView框架大小宽度调整为等于screenWidth。删除边框变量符合我完全适合屏幕的需要。
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
//let screenHeight = screenSize.height
let numWidth: CGFloat = 3
let numHeight: CGFloat = 10
self.scrollView.frame.size.width = screenWidth
let width: CGFloat = (self.scrollView.frame.size.width - (numWidth + 1))/3
for var i:CGFloat = 0; i < 3; ++i{
for var j:CGFloat = 0; j < 10; ++j {
let image: UIImage = UIImage(named: "image1.png")!
imageView = UIImageView(image: image)
imageView!.frame = CGRectMake(width*i, width*j, width, width)
self.scrollView.addSubview(imageView!)
}
}
scrollView.contentSize.height = (width)*numHeight
谢谢大家的帮助!