kalim sayyad
kalim sayyad

Reputation: 1086

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?

Upvotes: 64

Views: 157202

Answers (12)

Priyank Patel
Priyank Patel

Reputation: 880

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")

Upvotes: 5

Iker Solozabal
Iker Solozabal

Reputation: 1412

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?

Upvotes: 0

nicael
nicael

Reputation: 19024

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)

Upvotes: 80

shubham
shubham

Reputation: 631

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)

Upvotes: 5

ahmed
ahmed

Reputation: 31

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

Upvotes: -1

Pchaurasia
Pchaurasia

Reputation: 1

/*Swift-5 update*/

let tempBtn: UIButton = UIButton(frame: CGRect(x: 5, y: 20, width: 40, height: 40))       
tempBtn.backgroundColor = UIColor.red

Upvotes: 0

BPratt
BPratt

Reputation: 60

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)
}

Upvotes: 1

Md Imran Choudhury
Md Imran Choudhury

Reputation: 10057

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)
    }
}

Upvotes: -1

Developer Sheldon
Developer Sheldon

Reputation: 2160

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

Upvotes: 0

Erik Bylund
Erik Bylund

Reputation: 131

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

button.backgroundColor = UIColor.blue

the Color() has been removed.

Upvotes: 13

Vivek
Vivek

Reputation: 5223

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

Upvotes: -1

kalafun
kalafun

Reputation: 3671

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)

Upvotes: 34

Related Questions