Change button background color using swift language

前端 未结 11 2192
暗喜
暗喜 2020-12-08 13:09

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

相关标签:
11条回答
  • 2020-12-08 13:29

    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?

    0 讨论(0)
  • 2020-12-08 13:30

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:31

    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)
    
    0 讨论(0)
  • 2020-12-08 13:36

    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)
    
    0 讨论(0)
  • 2020-12-08 13:41

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

    button.backgroundColor = UIColor.blue
    

    the Color() has been removed.

    0 讨论(0)
  • 2020-12-08 13:42
    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)
    
    0 讨论(0)
提交回复
热议问题