简体   繁体   English

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

[英]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.

相关问题 如何更改圆形按钮上的边框颜色 (Swift) - How change border color on round button (Swift) 如何在 Swift 3 中突出显示(或点击)按钮时更改 UIButton 边框颜色? - How to change UIButton border color when button is highlighted (or tapped on) in Swift 3? 使用 swift 语言更改按钮背景颜色 - Change button background color using swift language 选择 SWIFT 时更改按钮背景颜色 - Change button background color when selected SWIFT 如何从另一个 function 访问 flutter 中的凸起按钮以更改该按钮边框或背景颜色? - How to access Raised Button in flutter from another function to change that button border or background color? 按下时快速更改按钮背景颜色和图像颜色 - Swift change button background color and image color while pressed 如何将UiSearchBar的背景颜色更改为黑色-SWIFT - How to change background color of UiSearchBar to black -SWIFT 如何在 Swift 中以编程方式更改自定义 UITableViewCell 中按钮的背景颜色? - How do I change the background color of a button within a custom UITableViewCell programatically in Swift? Swift:如何在tableViewController中以编程方式更改tableHeaderView的边框颜色 - Swift: How to change tableHeaderView's border color programatically in a tableViewController 如何在 Swift 中更改按钮标题的颜色 - How to change the color of the button title in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM