[英]Swift how change the background color and button border
I have function: 我有功能:
func setSelectedSystemButtonColor(hoverButton: Int){
let defaultColor = UIColor(red: 0/255, green: 62/255, blue: 132/255, alpha: 1)
let selectedColor = UIColor(red: 251/255, green: 186/255, blue: 8/255, alpha: 1)
homeBtn.backgroundColor = defaultColor
productsBtn.backgroundColor = defaultColor
calculatorBtn.backgroundColor = defaultColor
conceptBtn.backgroundColor = defaultColor
tipBtn.backgroundColor = defaultColor
if hoverButton == 1 {
homeBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 2 {
productsBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 3 {
calculatorBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 4 {
conceptBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 5 {
tipBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
}
This function is designed to: a) reset the color to default, b) set the background color for the selected button + add a border. 此功能旨在:a)将颜色重置为默认值,b)设置所选按钮的背景颜色+添加边框。
The colors change correctly, however the borders do not display. 颜色正确更改,但是不显示边框。 The borders would be on 5px 边框将在5px上
你忘了设置UIButton
边框
button.layer.borderWidth = 0.8
you should do it like: 您应该这样做:
homeBtn.layer.borderWidth = 5
homeBtn.layer.borderColor = defaultColor.cgColor
instead of simply doing: 而不是简单地做:
homeBtn.layer.borderColor = defaultColor.cgColor
Only Replace this line 仅替换此行
let defaultColor = UIColor(red: 0.0/255.0, green: 62.0/255.0, blue: 132.0/255.0, alpha: 1)
with 与
let selectedColor = UIColor(red: 251.0/255.0, green: 186.0/255.0, blue: 8.0/255.0, alpha: 1)
In order to show button border, first you have to explicitly give homeBtn
border width like this: 为了显示按钮边框,首先必须显式指定homeBtn
边框宽度,如下所示:
homeBtn.layer.borderWidth = 5.0
then only you can add color to homeBtn
border layer like this: 那么只有您可以像这样向homeBtn
边框图层添加颜色:
homeBtn.layer.borderColor = defaultColor.cgColor
You may need to reset border width all buttons. 您可能需要重置所有按钮的边框宽度。
func setSelectedSystemButtonColor(hoverButton: Int){
let defaultColor = UIColor(red: 0/255, green: 62/255, blue: 132/255, alpha: 1)
let selectedColor = UIColor(red: 251/255, green: 186/255, blue: 8/255, alpha: 1)
homeBtn.backgroundColor = defaultColor
productsBtn.backgroundColor = defaultColor
calculatorBtn.backgroundColor = defaultColor
conceptBtn.backgroundColor = defaultColor
tipBtn.backgroundColor = defaultColor
// reset border width of all buttons
homeBtn.layer.borderWidth = 0.0
productsBtn.layer.borderWidth = 0.0
calculatorBtn.layer.borderWidth = 0.0
conceptBtn.layer.borderWidth = 0.0
tipBtn.layer.borderWidth = 0.0
// set border width for selected button
hoverButton.layer.borderWidth = 1.0
if hoverButton == 1 {
homeBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 2 {
productsBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 3 {
calculatorBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 4 {
conceptBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
if hoverButton == 5 {
tipBtn.backgroundColor = selectedColor
homeBtn.layer.borderColor = defaultColor.cgColor
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.