ios 使用 swift 语言更改按钮背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24427284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change button background color using swift language
提问by kalim sayyad
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 语言的新手。有人能告诉我如何使用 swift 语言更改按钮的背景颜色吗?
回答by nicael
button.backgroundColor = UIColor.blue
Or any other color: red
, green
, yellow
,etc.
或任何其他颜色:red
,green
,yellow
等。
Another option is RGBA color:
另一种选择是 RGBA 颜色:
button.backgroundColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5)
回答by kalafun
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)
回答by Erik Bylund
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()
已被删除。
回答by shubham
If you want to set?backgroundColor?of button programmatically then this code will surly help you
如果您想以编程方式设置?backgroundColor?of 按钮,那么此代码将非常有帮助
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)
回答by BPratt
You can set the background color of a button using the getRed function.
Let's assume you have:
您可以使用 getRed 函数设置按钮的背景颜色。
让我们假设你有:
- A UIButton called button, and
- You want to change button's background color to some known RBG value
- 一个名为 button 的 UIButton,以及
- 您想将按钮的背景颜色更改为某个已知的 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)
}
回答by Pchaurasia
/*Swift-5 update*/
let tempBtn: UIButton = UIButton(frame: CGRect(x: 5, y: 20, width: 40, height: 40))
tempBtn.backgroundColor = UIColor.red
回答by Developer Sheldon
U can also play around the tintcolor and button image to indirectly change the color.
您还可以使用 tintcolor 和按钮图像来间接更改颜色。
回答by ahmed
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:
将要更改其背景的 UIButton 作为 OUTlet 连接到 ViewController.swift 文件后,您可以使用以下内容:
yourUIButton.backgroundColor = UIColor.blue
回答by Md Imran Choudhury
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)
}
}
回答by Vivek
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