I am new to the swift language. Can someone tell me how to change the background color of a button using the swift language?
To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.
Just scroll a little bit in Attribute Inspector , you can find Background property in view section that will allow you set the backgroundColor for the UIButton . If you want to set backgroundColor of button programatically.
button.backgroundColor = UIColor.blue
Or any other color: red
, green
, yellow
,etc.
Another option is RGBA color:
button.backgroundColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5)
Try this, you need to add the 255
like so:
button.backgroundColor = UIColor(red: 102/255, green: 250/255, blue: 51/255, alpha: 0.5)
Update for xcode 8 and swift 3, specify common colors like:
button.backgroundColor = UIColor.blue
the Color()
has been removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With