iosuibuttonuiedgeinsets

Different title origin of UIButton with image inside in different iOS versions


UIButton insets screenshot

The problem: In different iOS versions (in my case it’s: 10.3.1, 12.1 and 13.1) the button drawing differently. Button’s title origin is (x = 29.5, y = 11) for iOS 13.1 and (x = 44, y = 11) for older iOS.

Simulators screenshot

How to make buttons look identical in all iOS versions?


Solution

  • My research revealed a possible reason for such a different drawing of buttons. And the reason is imageView’s old frame. After adding image insets to move image to left / right, the image frame changes. I don’t know how exactly Apple calculates title's origin of button. In older iOS title’s origin.x is equal image’s old frame.width. In iOS 13 title's origin changes as I expected.

    So i decided to resize an UIImage before I put it into UIImageView. And it works pretty well.

    Thanks to everyone who gave advice.