gpt4 book ai didi

swift - 减少 swift UIView 中的冗余

转载 作者:行者123 更新时间:2023-11-28 08:58:08 25 4
gpt4 key购买 nike

无论如何同时将 horizo​​ntalLine 和 verticalLine 添加到 UIView。以下方式看起来多余。

var horizontalLineView : UIView = UIView(frame: horizontalLine)
var verticalLineView : UIView = UIView(frame: verticalLine)

最佳答案

不,你做对了。这是创建 UIView 的最简洁的方法。

可能,您可以创建一个非常简单的 UIView 扩展,在一次调用中创建多行,但您付出的努力并没有真正得到多少。

extension UIView {

static func createLines(frames:CGRect...) -> [UIView] {
// create empty array for views
var lines:[UIView] = []

// loop over each frame provided
for frame in frames {
// create a UIView with that frame
lines.append(UIView(frame: frame))
}
// return the lines
return lines
}

}

let horizontalLine = CGRect(x: 0, y: 0, width: 100, height: 1)
let verticalLine = CGRect(x: 0, y: 0, width: 1, height: 100)

// create the UIViews with one or more frames
let lines = UIView.createLines(horizontalLine, verticalLine)

关于swift - 减少 swift UIView 中的冗余,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541903/

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