Error404 Error404 - 1 year ago 116
iOS Question

Why am I not able to change UIButton text-color?

I am trying to change the text-color of an

programmatically on Swift 3.

I have done it in other buttons but there is only one in the whole project that I cannot change the color and I cannot find why (I can change it manually from storyboard but I need to change its color programmatically). To change the text-color in other buttons I have done the following:

customButton.tintColor =

and it works well. Except one
that I have inside an
in which I cannot change the text-color to that
programmatically. I can change it manually (using storyboard), although.

This is the code that I have for the custom

@IBOutlet weak var customButton: UIButton!

override func awakeFromNib() {

customButton.tintColor =
customButton.backgroundColor = UIColor.yellow

but it only changes the background color to yellow.

This is my storyboard configuration (that I do not know if it can be relevant for this purpose):

enter image description here

I know that the connection to that
is working well because the background-color is changing but then,

Why am I not able to change its text-color? Am I missing something?

Thanks in advance!

Answer Source


Setting tintColor works only for buttonType = .custom. When you create a button using UIButton() the default buttonType would be .system which will override your global titleLabel and tintColor due to configuration with specific states


If you want the behavior of the system UIButton of fading the titleLabel when pressing the button, you need to set the titleColor for each state like below:

let button = UIButton()
button.setTitleColor(, for: .normal)

Otherwise you can use .custom and set tintColor directly.

let button = UIButton(type: .custom)
button.tintColor =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download