Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change button background color using swift language

I am new to the swift language. Can someone tell me how to change the background color of a button using the swift language?

like image 622
kalim sayyad Avatar asked Jun 26 '14 09:06

kalim sayyad


People also ask

How do I change the background color of a button?

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.

How do I change the button background in Xcode?

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.


3 Answers

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)
like image 170
nicael Avatar answered Oct 16 '22 07:10

nicael


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)
like image 34
kalafun Avatar answered Oct 16 '22 06:10

kalafun


Update for xcode 8 and swift 3, specify common colors like:

button.backgroundColor = UIColor.blue

the Color() has been removed.

like image 13
Erik Bylund Avatar answered Oct 16 '22 07:10

Erik Bylund