Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 833
Misha Timofeev Avatar asked Mar 11 '23 12:03

Misha Timofeev


1 Answers

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.

like image 198
Sanoj Kashyap Avatar answered Mar 25 '23 21:03

Sanoj Kashyap