【发布时间】:2016-02-21 23:49:22
【问题描述】:
我正在创建一组简单的 UIImageView,它们都使用相同的图像并且在屏幕上垂直均匀分布。最初我只是对每个单独视图的创建进行硬编码,但我想让它更紧凑,因为它们都是相同的图像。
这是我当前的代码,XCode 抱怨 (EXC_BAD_INSTRUCTION)。 loadingView 是一个 UIView,用于将所有图片组合在一起。
let items = [UIImageView?](count:7, repeatedValue: nil)
for item in items {
item!.image = UIImage(named: "loading_items")
}
// could potentially iterate through this too
self.loadingView.addSubview(items[0]!)
items[0]!.autoPinEdgeToSuperviewEdge(.Top)
self.loadingView.addSubview(items[1]!)
items[1]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[0]!, withOffset: 12)
self.loadingView.addSubview(items[2]!)
items[2]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[1]!, withOffset: 12)
self.loadingView.addSubview(items[3]!)
items[3]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[2]!, withOffset: 12)
self.loadingView.addSubview(items[4]!)
items[4]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[3]!, withOffset: 12)
self.loadingView.addSubview(items[5]!)
items[5]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[4]!, withOffset: 12)
self.loadingView.addSubview(items[6]!)
items[6]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[5]!, withOffset: 12)
for item in items {
item!.autoAlignAxis(.Vertical, toSameAxisOfView: loadingView) // uses AutoLayout
}
我想知道这段代码有什么问题,以及是否有更高效和“Swift-y”的方式来做到这一点。
【问题讨论】:
标签: ios iphone swift uiimageview