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

时间:2022-11-20 14:02:57

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

我对快速语言很陌生。有人能告诉我如何使用swift语言改变按钮的背景颜色吗?

7 个解决方案

#1


54  

button.backgroundColor = UIColor.blueColor()

Or any other color: redColor(), greenColor(), yellowColor() ,etc.

或任何其他颜色:redColor()、greenColor()、yellowColor()等。

Another option is RGBA color:

另一种选择是RGBA颜色:

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

#2


21  

Try this, you need to add the 255 like so:

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

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

#3


8  

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

更新xcode 8和swift 3,指定常见颜色如下:

button.backgroundColor = UIColor.blue

the Color() has been removed.

颜色()已被删除。

#4


1  

You can set the background color of a button using the getRed function.
Let's assume you have:

可以使用getRed函数设置按钮的背景颜色。假设你有:

  1. A UIButton called button, and
  2. 一个叫做按钮的UIButton
  3. You want to change button's background color to some known RBG value
  4. 您需要将按钮的背景颜色更改为已知的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

    let color = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0) // This will be some red-ish color
    if color.getRed(&r, green: &g, blue: &b, alpha: &a){
        button.backgroundColor = UIColor(red: r, green: g, blue: b, alpha: a)
    }

#5


0  

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

U也可以围绕着丁色和按钮图像间接地改变颜色。

#6


0  

Swift 4:

斯威夫特4:

You can easily use hex code:

您可以轻松地使用十六进制代码:

self.backgroundColor = UIColor(hexString: "#ff259F6C")

self.layer.shadowColor = UIColor(hexString: "#08259F6C").cgColor

#7


-1  

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

#1


54  

button.backgroundColor = UIColor.blueColor()

Or any other color: redColor(), greenColor(), yellowColor() ,etc.

或任何其他颜色:redColor()、greenColor()、yellowColor()等。

Another option is RGBA color:

另一种选择是RGBA颜色:

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

#2


21  

Try this, you need to add the 255 like so:

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

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

#3


8  

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

更新xcode 8和swift 3,指定常见颜色如下:

button.backgroundColor = UIColor.blue

the Color() has been removed.

颜色()已被删除。

#4


1  

You can set the background color of a button using the getRed function.
Let's assume you have:

可以使用getRed函数设置按钮的背景颜色。假设你有:

  1. A UIButton called button, and
  2. 一个叫做按钮的UIButton
  3. You want to change button's background color to some known RBG value
  4. 您需要将按钮的背景颜色更改为已知的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

    let color = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0) // This will be some red-ish color
    if color.getRed(&r, green: &g, blue: &b, alpha: &a){
        button.backgroundColor = UIColor(red: r, green: g, blue: b, alpha: a)
    }

#5


0  

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

U也可以围绕着丁色和按钮图像间接地改变颜色。

#6


0  

Swift 4:

斯威夫特4:

You can easily use hex code:

您可以轻松地使用十六进制代码:

self.backgroundColor = UIColor(hexString: "#ff259F6C")

self.layer.shadowColor = UIColor(hexString: "#08259F6C").cgColor

#7


-1  

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