突出显示/选定状态问题的UIButton背景颜色 [英] UIButton background color for highlighted/selected state issue

查看:321
本文介绍了突出显示/选定状态问题的UIButton背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIButton ,我创建了一个扩展,为不同的状态添加背景颜色。

I've a UIButton and I've created an extension to add background color for different state.

我使用以下代码:

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)




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

My Problem is that it is not picking up proper UIButton background color and title color for highlighted/selected state.


推荐答案

我发现问题, UIButton 设置为系统。我只是将其更改为自定义,它开始按预期工作。

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

这篇关于突出显示/选定状态问题的UIButton背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆