简体   繁体   English

突出显示/选定状态问题的 UIButton 背景颜色

[英]UIButton background color for highlighted/selected state issue

I've a UIButton and I've created an extension to add background color for different state.我有一个UIButton并且我创建了一个扩展来为不同的状态添加背景颜色。

I'm using the following code:我正在使用以下代码:

extension UIButton {

    func setBackgroundColor(color: UIColor, forState: UIControlState) {

        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
        CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
        let colorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        self.setBackgroundImage(colorImage, forState: forState)
    }
}


// Set Button Title and background color for different states
    self.donateButton.setBackgroundColor(UIColor.redColor(), forState: .Normal)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Highlighted)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
    self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Selected)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Selected)

My Problem is that it is not picking up proper UIButton background color and title color for highlighted/selected state.我的问题是它没有为突出显示/选定状态选择正确的UIButton背景颜色和标题颜色。

正常状态 高亮状态

我发现了这个问题, UIButton被设置为System 。我只是将其更改为Custom ,它开始按预期工作。

Update Swift 4更新 Swift 4

extension UIButton {

    func setBackgroundColor(color: UIColor, forState: UIControlState) {

        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
        UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        let colorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        self.setBackgroundImage(colorImage, for: forState)
    }

}

Event Button Action Show Touch On Highlight事件按钮动作显示触摸高亮

Swift 5, IOS 13+斯威夫特 5, IOS 13+

extension UIButton {

  func setBackgroundColor(_ color: UIColor, for forState: UIControl.State) {
    UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
    UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
    UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    self.setBackgroundImage(colorImage, for: forState)
  }

}

The reason it does not work is that you have not set the correct state: you are missing the [ .highlighted, .selected ] state, and so it falls back to plain .normal .它不起作用的原因是您没有设置正确的状态:您缺少[ .highlighted, .selected ]状态,因此它回落到普通的.normal

When both isSelected and isHighlighted are true, UIKit looks explicitly for the state-combination of the two, and will not accept any of them individually.isSelectedisHighlighted都为 true 时,UIKit 会明确查找两者的状态组合,并且不会单独接受它们中的任何一个。

The original code extended with the missing state (and updated to valid syntax in Swift 5):原始代码扩展了缺少的状态(并在 Swift 5 中更新为有效语法):

self.donateButton.setBackgroundColor(.green, for: [ .highlighted, .selected ])
self.donateButton.setTitleColor(.white, for: [ .highlighted, .selected ])

When your UIButton is selected.当您的UIButton被选中时。 If you focus on the button, it will not highlight the button as state .Highlighted .如果您关注按钮,它不会将按钮突出显示为 state .Highlighted If it is not selected, it will highlight it as your will.如果它没有被选中,它会根据你的意愿突出显示它。 For it regards you have background image in your button, it will use the default effect to highlight a image.因为它认为您的按钮中有背景图像,它将使用默认效果来突出显示图像。 If you want to change the state to .Selected , you have to set the variable selected of UIButton to true.如果要将状态更改为.Selected ,则必须将UIButton selected变量设置为 true。

要实现自定义按钮照明,您需要: - 在故事板中设置您想要看到的背景颜色和 alpha,当按钮被按下时 - 在初始化时,将背景和 alpha 设置为未按下的按钮 - 实施按钮的三种方法(Touch Down / Touch Up Side / Touch Drag Outside) - 在其中,更改按钮照明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton:为选定的突出显示的UI状态设置背景色(NOT IMAGE) - UIButton: set background color (NOT IMAGE) for selected-highlighted UI state UIButton的标题颜色在突出显示/选中时不会改变,但背景颜色会改变 - Title color of UIButton won't change on highlighted/selected but background color will ObjectiveC - 当突出显示/选择时,UIButton保持突出显示/选中,背景颜色和字体颜色会发生变化 - ObjectiveC - UIButton remains highlighted/selected and background color and font color changes when highlighted/selected UIButton:背景图像设置突出显示按钮状态时出现问题? - UIButton : Background Image setting Issue when button state is highlighted? UIButton的颜色会影响处于选定状态的按钮的背景色 - UIButton tint color affected the background color of the button in selected state UIButton背景图像突出显示状态错误 - UIButton with background image highlighted state wrong 以编程方式为UIButton创建突出显示的状态背景图像 - Create highlighted state background image programmatically for UIButton UIButton自定义背景色突出显示并禁用 - UIButton custom background color highlighted and disabled 点击选定的UIButton时,UIButton突出显示的状态不显示 - UIButton Highlighted State not showing when clicking over a Selected UIButton 在选定状态下更改UIButton的颜色 - Change color of UIButton in selected state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM