未选中时无法更改按钮颜色 [英] can't change the button color when not selected

查看:23
本文介绍了未选中时无法更改按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多自定义按钮,我想为选中和未选中的状态设置颜色.

i have many custom buttons, and i want to set a color for state selected and not selected.

这是我的画框

   override func drawRect(rect: CGRect) {
        print("status = \(self.selected)")
        if self.selected {
            self.titleLabel?.textColor = UIColor(red: 255.0, green: 255.0, blue: 255.0, alpha: 1.0)
        }else {
            self.titleLabel?.textColor = UIColor(red: 166.0, green: 142.0, blue: 83.0, alpha: 1.0)
        }

        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: self.bounds.minX, y: self.bounds.maxY))
        path.addLineToPoint(CGPoint(x: self.bounds.maxX, y: self.bounds.maxY))
        path.closePath()
//        path.addLineToPoint(center)
        UIColor.blackColor().setStroke()
        path.lineWidth = 3.0
        path.stroke()
    }

   private var isSelectedValue = false
    var isThisButtonSelected : Bool {
        get {
            return isSelectedValue
        }
        set {
            isSelectedValue = newValue
            selected = newValue
            switch newValue {
            case true:

                self.selected = true
                rightImageView?.image = UIImage(named: "selection-preferences")
                break;
            case false:
                self.selected = false

                rightImageView?.image = nil
                break;
            }
        }
    }

我有很多按钮,如你所见,我打印了选定的状态.

i have many buttons, and as you see, i print the selected status.

结果总是错误的(你会在截图中看到)

the result is always false (as you will see in the screenshot)

我的问题是未选择时颜色为白色,即使我在绘图矩形中说如果不选择,请制作特定颜色

my problem is that the color is white when not selected even though i say in the draw rect that if not select, make specific color

但是当我选择一个按钮使颜色变为白色时效果很好,如您所见,但是为什么当我取消选择颜色时不会改变?

but it work good when i select a button so the color becomes white, as you see, but why when i unselect the color not change?

当用户点击按钮时,我在我的视图控制器中执行此操作

when the user click on the button, i do this in my view controller

 @IBAction func foodTabled(sender: PreferenceButton) {
        sender.isThisButtonSelected = !sender.isThisButtonSelected
    }

推荐答案

与 alpha 一样,UIColor 的红/绿/蓝值介于 0.0 和 1.0 之间.您必须将您的值除以 255.0.

Like alpha, the red/green/blue values to UIColor are between 0.0 and 1.0. You must divide your values by 255.0.

--

除了 setTitleColor:forState: 之外,您还有 setBackgroundImage:forState:,用于控制该附加图像的存在与否(尽管您会重构该图像覆盖整个背景).通过明智地使用这两种方法,您可以一起简化这个按钮类,停用 isSelectedValue 并只使用现有的 selected 属性.

In addition to setTitleColor:forState:, you also have setBackgroundImage:forState:, to control the the presence or absence of that additional image (though you'd refactor that image to cover the whole background). With judicious use of those two methods, you can probably streamline this button class, altogether, retiring isSelectedValue and just using the existing selected property.

此外,您可以将按钮下方的那条线渲染为按钮的纯色子视图.如果你这样做了,你也可以退休 drawRect.

Furthermore, you can render that line under the button as a solid-colored subview of the button. If you did that, you could retire drawRect, too.

这篇关于未选中时无法更改按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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