2016-12-20 30 views
0

我有一個圖像和文字都在其中的UIButton。除了被按下時,我對它的外觀很滿意。如何使UIButton具有正確的圖像和文字外觀?

它被按下之前,它看起來是這樣的: enter image description here

它被按下後,它看起來是這樣的:當按下 enter image description here

我用外觀昏暗,整個按鈕,但對於一些當我同時擁有圖像和文字的原因是指定圖像的所有着色。我很清楚,我可以讓整個按鈕成爲一個圖像,但我試圖讓這個項目儘可能遠離這種風格。我的其他按鈕都是在沒有圖像的情況下創建的。

有沒有什麼辦法讓整個按鈕變暗,因爲它通常會像當前設置一樣?

回答

1

您必須設置UIButton的自定義類型(無系統類型),並更改屬性:

self.yourButton.adjustsImageWhenHighlighted = false 

如果我得到你想要與該代碼嘗試什麼:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.button.backgroundColor = UIColor.redColor() 
} 


@IBAction func buttonClicked(sender: UIButton) { 
    //Touch Up Inside action 
    sender.backgroundColor = UIColor.redColor() 
} 

@IBAction func buttonReleased(sender: UIButton) { 
    //Touch Down action 
    sender.backgroundColor = UIColor.grayColor() 

} 

正常:

enter image description here

追問:

enter image description here

+0

這可以防止按鈕從突出的。我想讓整個按鈕變暗,就像整個按鈕是圖像時一樣。 – Sethmr

+0

另一種選擇是使文本與圖像一起高亮的任何方法。 – Sethmr

+0

你能發表一個你想要的例子嗎? –

1

最簡單的解決辦法是匹配的文本的高亮顯示顏色:

button.setTitleColor(UIColor.gray, for: UIControlState.highlighted) 
+0

是的,出於某種原因,我認爲這是我認爲這在過去看起來不穩定,但現在我正在深入挖掘按鈕,這是一個合理的解決方案。 – Sethmr