我对Swift和Xcode 6很新,只是制作了一个应用程序。现在我正在尝试改进我的代码,我知道有一种方法可以缩短代码,我只是不知道如何。
button1.layer.cornerRadius = 10
button1.layer.borderWidth = 1
button1.layer.borderColor = UIColor.whiteColor().CGColor
button2.layer.cornerRadius = 10
button2.layer.borderWidth = 1
button2.layer.borderColor = UIColor.whiteColor().CGColor
button3.layer.cornerRadius = 10
button3.layer.borderWidth = 1
button3.layer.borderColor = UIColor.whiteColor().CGColor
button4.layer.cornerRadius = 10
button4.layer.borderWidth = 1
button4.layer.borderColor = UIColor.whiteColor().CGColor
button5.layer.cornerRadius = 10
button5.layer.borderWidth = 1
button5.layer.borderColor = UIColor.whiteColor().CGColor
答案 0 :(得分:4)
您可以创建所有按钮的数组,然后循环遍历它们。
var buttons = [button1, button2, button3, button4, button5]
for button in buttons {
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.whiteColor().CGColor
}