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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38562379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:45:52  来源:igfitidea点击:

UIButton background color for highlighted/selected state issue

iosswiftuibutton

提问by Ashish Verma

I've a UIButtonand 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 UIButtonbackground color and title color for highlighted/selected state.

我的问题是它没有UIButton为突出显示/选定状态选择正确的背景颜色和标题颜色。

Normal stateHighlighted state

正常状态高亮状态

回答by Ashish Verma

I found the issue, UIButtonwas set to System.I simply changed it to Custom, it started working as expected.

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

回答by Puji Wahono

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

事件按钮动作显示触摸高亮

回答by KuDji

To implement custom button illumination, you need: - Set in the storyboard the background color and alpha that you want to see, when button is pressed - At the time of initialization, set the background and alpha as a button that is not pressed -Implement three methods for the button (Touch Down / Touch Up Side / Touch Drag Outside) - In them, change the button illumination

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

回答by Mike Zriel

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

}

回答by u5755305

When your UIButtonis selected. If you focus on the button, it will not highlight the button as 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 selectedof UIButtonto true.

当您UIButton被选中时。如果您关注按钮,它不会将按钮突出显示为 state .Highlighted。如果它没有被选中,它会根据你的意愿突出显示它。因为它认为您的按钮中有背景图像,它将使用默认效果来突出显示图像。如果您想将状态改为.Selected,你必须设置变量selectedUIButton设置为true。