Change button background color using swift language

IosIphoneColorsUibuttonSwift

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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)

Solution 3 - Ios

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

button.backgroundColor = UIColor.blue

the Color() has been removed.

Solution 4 - Ios

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)

Solution 5 - Ios

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

Solution 6 - Ios

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

Solution 7 - Ios

/*Swift-5 update*/

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

Solution 8 - Ios

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:

https://stackoverflow.com/questions/26311000/how-to-connect-viewcontroller-swift-to-viewcontroller-in-storyboard/64246814#64246814

Solution 9 - Ios

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

Solution 10 - Ios

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

Solution 11 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkalim sayyadView Question on Stackoverflow
Solution 1 - IosnicaelView Answer on Stackoverflow
Solution 2 - IoskalafunView Answer on Stackoverflow
Solution 3 - IosErik BylundView Answer on Stackoverflow
Solution 4 - IosshubhamView Answer on Stackoverflow
Solution 5 - IosBPrattView Answer on Stackoverflow
Solution 6 - IosDeveloper SheldonView Answer on Stackoverflow
Solution 7 - IosPchaurasiaView Answer on Stackoverflow
Solution 8 - IosIker SolozabalView Answer on Stackoverflow
Solution 9 - IosVivekView Answer on Stackoverflow
Solution 10 - IosMd Imran ChoudhuryView Answer on Stackoverflow
Solution 11 - IosahmedView Answer on Stackoverflow