Swift如何更改背景颜色和按钮边框

时间:2018-05-24 09:30:20

标签: ios swift

我有功能:

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
    }
}

此功能旨在: a)将颜色重置为默认值, b)设置所选按钮的背景颜色+添加边框。

颜色正确更改,但不显示边框。 边界将是5px

5 个答案:

答案 0 :(得分:1)

您忘了设置UIButton边框

button.layer.borderWidth = 0.8

答案 1 :(得分:0)

你应该这样做:

  homeBtn.layer.borderWidth = 5
  homeBtn.layer.borderColor = defaultColor.cgColor

而不是简单地做:

  homeBtn.layer.borderColor = defaultColor.cgColor

答案 2 :(得分:0)

仅替换此行

let defaultColor = UIColor(red: 0.0/255.0, green: 62.0/255.0, blue: 132.0/255.0, alpha: 1)

let selectedColor = UIColor(red: 251.0/255.0, green: 186.0/255.0, blue: 8.0/255.0, alpha: 1)

答案 3 :(得分:0)

要显示按钮边框,首先必须明确指定homeBtn边框宽度,如下所示:

homeBtn.layer.borderWidth = 5.0

然后只有你可以向homeBtn边框图层添加颜色,如下所示:

homeBtn.layer.borderColor = defaultColor.cgColor

答案 4 :(得分:0)

您可能需要重置所有按钮的边框宽度。

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
    }
}