uibutton 数组如何分别应用不同的颜色
How array of uibutton to apply different colors each
我想在不同的 uiButton 应用不同的颜色(通过代码)。
我想创建 2 个数组。第一个是我的 uiButtons,另一个是不同的颜色。
之后,用我的按钮进行第一个循环,并在这个循环内,用颜色进行另一个循环(随机结果)。最后一步,在按钮上应用颜色。
这样可以吗?
您可以通过使用 this answer.
来做到这一点
let numberOfButton = 10
var buttons = [UIButton]()
for _ in 0..<numberOfButton {
let button = UIButton()
button.setTitleColor(.random(), for: .normal)
buttons.append(button)
}
您可以尝试 this.Its 工作代码,只需从 viewDidLoad 调用方法并查看它是如何工作的
func createButtonsWithDifferentTitleColor(){
// create array of different colors
let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple]
for i in 0...colorArray.count-1 {
let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50))
//set different title colors for each button
button.setTitleColor(colorArray[i], for: .normal)
// set different title for button
button.setTitle("Button No \(i+1)", for: .normal)
// add all buttons to main view, so that they can be visible
view.addSubview(button)
}
}
要设置随机颜色,您需要这样做
func createButtonsWithDifferentTitleColor(){
// create array of different colors
let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple]
for i in 0...colorArray.count-1 {
let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50))
// generate random index
let index = Int(arc4random_uniform(UInt32(colorArray.count)));
//set different title color for each button
button.setTitleColor(colorArray[index], for: .normal)
// set different title for button
button.setTitle("Button No \(i+1)", for: .normal)
// add all buttons to main view, so that they can be visible
view.addSubview(button)
}
}
我想在不同的 uiButton 应用不同的颜色(通过代码)。 我想创建 2 个数组。第一个是我的 uiButtons,另一个是不同的颜色。 之后,用我的按钮进行第一个循环,并在这个循环内,用颜色进行另一个循环(随机结果)。最后一步,在按钮上应用颜色。 这样可以吗?
您可以通过使用 this answer.
来做到这一点let numberOfButton = 10
var buttons = [UIButton]()
for _ in 0..<numberOfButton {
let button = UIButton()
button.setTitleColor(.random(), for: .normal)
buttons.append(button)
}
您可以尝试 this.Its 工作代码,只需从 viewDidLoad 调用方法并查看它是如何工作的
func createButtonsWithDifferentTitleColor(){
// create array of different colors
let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple]
for i in 0...colorArray.count-1 {
let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50))
//set different title colors for each button
button.setTitleColor(colorArray[i], for: .normal)
// set different title for button
button.setTitle("Button No \(i+1)", for: .normal)
// add all buttons to main view, so that they can be visible
view.addSubview(button)
}
}
要设置随机颜色,您需要这样做
func createButtonsWithDifferentTitleColor(){
// create array of different colors
let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple]
for i in 0...colorArray.count-1 {
let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50))
// generate random index
let index = Int(arc4random_uniform(UInt32(colorArray.count)));
//set different title color for each button
button.setTitleColor(colorArray[index], for: .normal)
// set different title for button
button.setTitle("Button No \(i+1)", for: .normal)
// add all buttons to main view, so that they can be visible
view.addSubview(button)
}
}