Search code examples
iosswiftuibuttonimageedgeinsetstitleedgeinsets

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


Solution

  • 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.