简体   繁体   English

使用 swift 语言更改按钮背景颜色

[英]Change button background color using swift language

I am new to the swift language.我是 swift 语言的新手。 Can someone tell me how to change the background color of a button using the swift language?有人可以告诉我如何使用 swift 语言更改按钮的背景颜色吗?

button.backgroundColor = UIColor.blue

Or any other color: red , green , yellow ,etc.或任何其他颜色: redgreenyellow等。

Another option is RGBA color:另一种选择是 RGBA 颜色:

button.backgroundColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5)

试试这个,你需要像这样添加255

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

Update for xcode 8 and swift 3, specify common colors like:更新 xcode 8 和 swift 3,指定常用颜色,如:

button.backgroundColor = UIColor.blue

the Color() has been removed. Color()已被删除。

If you want to set backgroundColor of button programmatically then this code will surly help you如果你想以编程方式设置按钮的背景颜色,那么这段代码会很好地帮助你

Swift 3 and Swift 4斯威夫特 3 和斯威夫特 4

self.yourButton.backgroundColor = UIColor.red

Swift 2.3 or lower Swift 2.3 或更低版本

self.yourButton.backgroundColor = UIColor.redColor()

Using RGB使用 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)

You can set the background color of a button using the getRed function.您可以使用 getRed 函数设置按钮的背景颜色。
Let's assume you have:假设您有:

  1. A UIButton called button, and一个称为按钮的 UIButton,以及
  2. You want to change button's background color to some known RBG value您想将按钮的背景颜色更改为某个已知的 RBG 值

Then, place the following code in the function where you want to change the background color of your UIButton然后,将以下代码放在要更改 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)
}

Swift 4,5 Add background color using number of ways. Swift 4,5 使用多种方式添加背景颜色。

1.Set button Color with below code 1.使用以下代码设置按钮颜色

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

2.Using RGB color: red, green, yellow ,etc. 2.使用RGB颜色:红色,绿色,黄色等。

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

3.Using 255 like below 3.使用 255 如下

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

4.Using UIColor Extension like below 4.使用 UIColor 扩展如下

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

U 还可以围绕 tintcolor 和 button image 来间接改变颜色。

/*Swift-5 update*/

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

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.如果您使用storyBoard ,请确保您已将您的storyBoard与您的viewController连接,并且您的项目已链接。

if you don´t know how to do this check the next link:如果您不知道如何执行此操作,请查看下一个链接:

How to connect ViewController.swift to ViewController in Storyboard? 如何将 ViewController.swift 连接到 Storyboard 中的 ViewController?

Swift 3斯威夫特 3

Color With RGB RGB 颜色

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

Using Native color使用本机颜色

btnLogin.backgroundColor = UIColor.darkGray

Swift 4:斯威夫特 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)
    }
}

将要更改其背景的 UIButton 作为 OUTlet 连接到 ViewController.swift 文件后,您可以使用以下内容:

 yourUIButton.backgroundColor = UIColor.blue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM