I’m trying to center the text of UIButton programmatically.
This is what I want the UIButton to look like (I used the interface builder):
This is what the UIButton looks like after I set it programmatically:
And this is what the UIButton looks like after I set it programmatically and tried to center the text like in the first picture:
This is the code that I used to try to center it:
previousPageButton.titleLabel?.numberOfLines = 0
previousPageButton.titleLabel?.textAlignment = .center
2
Answers
The problem was I had set up "attributed" to the title in interface builder. I just had to set it to "plain" and it’s working now.
Do it with new button configuration, set your button:
in viewDidLoad set constraints:
This is the result: