Misha Timofeev
Misha Timofeev

Reputation: 105

How to remove left padding from UIButton?

How to remove left padding from UIButton?

I have been created button with this code:

let button = UIButton(...)
button.setImage(UIImage(named: "plus")?.withRenderingMode(.alwaysTemplate), for: .normal)
button.setTitle("Text", for: .normal)
button.layer.cornerRadius = 8
button.layer.backgroundColor = UIColor.red.cgColor

UIButton

Upvotes: 6

Views: 4808

Answers (2)

Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

You need to adjust the imageEdgeInsets and titleEdgeInsets with some negative left value. Then it will shift to left. I tested it's working. 100 is temp value.

button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)

Let me know if it is not working.

Upvotes: 6

Kevin Sabbe
Kevin Sabbe

Reputation: 1452

This line could fix your issue

button.titleEdgeInsets.left = 0 // add left padding.

Or maybe you could use negative value in this case

There is also this way :

button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

Just try different values !

Upvotes: 1

Related Questions