我正在尝试使用以下代码修改Swift中UIButton的颜色:
struct ColorWheel {
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
func randomColor() -> UIColor {
var unsignedArrayCount = UInt32(colorsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return colorsArray[randomNumber]
}
}
var colorWheel = ColorWheel()
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
myButton.setTitleColor(randomColor, forState: .Normal)
此代码在更改背景颜色时可以正常工作,但是当我尝试使用setTitleColor
方法时,我收到以下错误:
(UIButton) -> 0' does not have a member named 'setTitleColor'