2016-03-02 33 views
0

如何避免使用UIControlEvents上的選擇器更改UIButton選擇上的文字顏色?如何通過UIControlEvents上的選擇器避免在UIButton選擇上更改文本顏色?

我試圖確保我的按鈕的文字顏色不會改變,當它被強調,選擇或正常。由於我的選擇器,它不起作用。我也嘗試在選擇器中更改titleLabel的文本顏色,但它不起作用。正如你所看到的,我需要改變backgroundlighlited時的背景顏色...添加控制事件目標是我發現改變選擇背景顏色的唯一方法。

這裏是我的按鈕配置代碼:

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

任何其他工作的選擇,歡迎選購。提前致謝。

+0

你真的想要一個按鈕嗎?看起來你可以用互動標籤做所有你需要的東西 – Russell

+0

是的,我真的需要一個按鈕 – AlexB

回答

2

這看起來不錯,你只需要確保你的按鈕類型是自定義的。

let btn = UIButton(type: .Custom) 
+0

釘牢它。正是我所期待的。 – AlexB