I am new to the swift language. Can someone tell me how to change the background color of a button using the swift language?
12 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)
2 Comments
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)
2 Comments
115/255Update for xcode 8 and swift 3, specify common colors like:
button.backgroundColor = UIColor.blue
the Color() has been removed.
1 Comment
button.backgroundColor = .blue (because the compiler knows that .backgroundColor is a UIColor).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)
Comments
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")
Comments
You can set the background color of a button using the getRed function.
Let's assume you have:
- A UIButton called button, and
- 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)
}
1 Comment
button.backgroundColor = color.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?
Comments
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)
}
}