简体   繁体   中英

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.

The colors change correctly, however the borders do not display. The borders would be on 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.layer.borderWidth = 5.0

then only you can add color to homeBtn border layer like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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