如何避免使用UIControlEvents上的选择器更改UIButton选择的文本颜色?

时间:2016-03-02 07:40:42

标签: ios swift uibutton

如何避免使用UIControlEvents上的选择器更改UIButton选择时的文本颜色?

我正在努力确保我的按钮的文字颜色在突出显示,选择或正常时不会发生变化。因为我的选择器,它不起作用。我也尝试在选择器中更改titleLabel的文本颜色,但它没有用。正如你所看到的那样,我需要在高亮度时更改背景颜色...添加控制事件目标是我发现在选择时更改背景颜色的唯一方法。

这是我的按钮配置代码:

func setBtn(btn: UIButton) {
    setBtnTarget(btn)
    btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    btn.setTitleColor(UIColor.whiteColor(), forState:  UIControlState.Highlighted )
    btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
}

func setBtnTarget(btn: UIButton) {
    btn.addTarget(self, action: "highlighted:", forControlEvents: UIControlEvents.TouchDown)
    btn.addTarget(self, action: "normal:", forControlEvents: UIControlEvents.TouchUpInside)
}

func highlighted(btn : UIButton) {
    btn.backgroundColor = UIColor(red:0, green:0.133, blue:0.229, alpha:1)
}
func normal(btn : UIButton) {
    btn.backgroundColor = UIColor(red:0, green:0.058, blue:0.099, alpha:1)
}

欢迎任何其他工作方式。提前谢谢。

1 个答案:

答案 0 :(得分:2)

这看起来不错,您只需要确保您的按钮类型是自定义的。

let btn = UIButton(type: .Custom)
相关问题