63

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

pjtnt11
  • 495
  • 2
  • 5
  • 22
kalim sayyad
  • 1,076
  • 1
  • 9
  • 13

12 Answers12

79
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)
themenace
  • 2,601
  • 2
  • 20
  • 33
nicael
  • 18,550
  • 13
  • 57
  • 90
  • My related question is: the color fills the button's bounding box--- how do you only fill the shape of the button (i.e., the standard system button that has rounded corners)? – Bryan Green Jun 12 '22 at 02:13
  • Bear in mind, yourButton.setBackgroundColor(...) is something different. Use this answer. – Wyatt Dec 08 '22 at 23:36
34

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)
Bruno Galinari
  • 155
  • 2
  • 12
kalafun
  • 3,512
  • 6
  • 35
  • 49
  • 4
    not quite correct, you need a value between 0.0 and 1.0. So either you use such values or write `115/255` – MMachinegun Mar 12 '15 at 18:00
  • thanks, dividing by 255 fixed my error in Swift4. sender.backgroundColor = UIColor(red: 3/255, green: 122/255, blue: 255/255, alpha: 1) – sf2k Sep 15 '17 at 02:52
13

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

button.backgroundColor = UIColor.blue

the Color() has been removed.

Alien
  • 3,658
  • 1
  • 16
  • 33
Erik Bylund
  • 131
  • 1
  • 5
  • 4
    Even better: `button.backgroundColor = .blue` (because the compiler knows that `.backgroundColor` is a UIColor). – Eric Aya Oct 10 '16 at 17:03
5

If you want to set backgroundColor of button programmatically then this code will surly help you

Swift 3 and Swift 4

self.yourButton.backgroundColor = UIColor.red

Swift 2.3 or lower

self.yourButton.backgroundColor = UIColor.redColor()

Using RGB

self.yourButton.backgroundColor = UIColor(red: 102/255, green: 250/255, blue: 51/255, alpha: 0.5)

or you can use float values

button.backgroundColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5)
shubham
  • 611
  • 7
  • 16
5

Swift 4,5 Add background color using number of ways.

1.Set button Color with below code

button.backgroundColor = UIColor.blue 
**OR**
button.backgroundColor = .blue

2.Using RGB color: red, green, yellow ,etc.

button.backgroundColor = UIColor(red: 0.1, green: 0.9, blue: 1.0, alpha: 1.0)

3.Using 255 like below

button.backgroundColor = UIColor(red: 203/255, green: 95/255, blue: 173/255, alpha: 1.0)

4.Using UIColor Extension like below

extension UIColor {
    convenience init(hex: String) {
        let scanner = Scanner(string: hex)
        scanner.scanLocation = 0
        
        var rgbColorValue: UInt64 = 0
        
        scanner.scanHexInt64(&rgbColorValue)
        
        let r = (rgbColorValue & 0xff0000) >> 16
        let g = (rgbColorValue & 0xff00) >> 8
        let b = rgbColorValue & 0xff
        
        self.init(
            red: CGFloat(r) / 0xff,
            green: CGFloat(g) / 0xff,
            blue: CGFloat(b) / 0xff, alpha: 1
        )
    }
}

How to use

button.backgroundColor = UIColor(hex:"FFFFFF")
Priyank Patel
  • 791
  • 8
  • 6
1

You can set the background color of a button using the getRed function.
Let's assume you have:

  1. A UIButton called button, and
  2. You want to change button's background color to some known RBG value

Then, place the following code in the function where you want to change the background color of your UIButton

var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0

// This will be some red-ish color
let color = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0) 

if color.getRed(&r, green: &g, blue: &b, alpha: &a){
    button.backgroundColor = UIColor(red: r, green: g, blue: b, alpha: a)
}
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
BPratt
  • 60
  • 1
  • 5
0

U can also play around the tintcolor and button image to indirectly change the color.

Developer Sheldon
  • 2,140
  • 1
  • 11
  • 17
0
/*Swift-5 update*/

let tempBtn: UIButton = UIButton(frame: CGRect(x: 5, y: 20, width: 40, height: 40))       
tempBtn.backgroundColor = UIColor.red
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

To change your background color of the botton use:

yourBtn.backgroundColor = UIColor.black

if you are using storyBoard make sure you have connected your storyBoard with your viewController and also that your items are linked.

if you don´t know how to do this check the next link:

How to connect ViewController.swift to ViewController in Storyboard?

Iker Solozabal
  • 1,232
  • 11
  • 16
-1

Swift 3

Color With RGB

btnLogin.backgroundColor = UIColor.init(colorLiteralRed: (100/255), green: (150/255), blue: (200/255), alpha: 1)

Using Native color

btnLogin.backgroundColor = UIColor.darkGray
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
-1

Swift 4:

You can easily use hex code:

self.backgroundColor = UIColor(hexString: "#ff259F6C")

self.layer.shadowColor = UIColor(hexString: "#08259F6C").cgColor

Extension for hex color code

extension UIColor {
    convenience init(hexString: String, alpha: CGFloat = 1.0) {
        let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        let scanner = Scanner(string: hexString)
        if (hexString.hasPrefix("#")) {
            scanner.scanLocation = 1
        }
        var color: UInt32 = 0
        scanner.scanHexInt32(&color)
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
        self.init(red:red, green:green, blue:blue, alpha:alpha)
    }
    func toHexString() -> String {
        var r:CGFloat = 0
        var g:CGFloat = 0
        var b:CGFloat = 0
        var a:CGFloat = 0
        getRed(&r, green: &g, blue: &b, alpha: &a)
        let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0
        return String(format:"#%06x", rgb)
    }
}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
-1

After you connect the UIButton that you want to change its background as an OUtlet to your ViewController.swift file you can use the following:

 yourUIButton.backgroundColor = UIColor.blue
ahmed
  • 21
  • 3