Earl Grey Earl Grey - 1 year ago 52
Swift Question

How to fix the position of image inside UIButton?

Let's say we have an image with size 22 x 22. We also have a button with size 260 x 44.
This button will be resized horizontally so that it is always 16 px from the edges of screen.

I want to fix the position of the image inside the button so that it is vertically centered and 10 px from the left button edge, regardless of the button size in design time. This button will be resized to fit iPhone 5/6/6+

I don't want to calculate content/image/title/edgeInsets for this button outside the button in some controller's viewDidLayoutSubviews that feels wrong. This is not VCs responsibility.
I am not going to subclass

, it's a class cluster, all right?

Am I really stuck with implementing my own Button from UIControl?
Or did I miss some possible configuration of UIButton class?


With contentHorizontalAlignment you can set the alignment of the image to the left, with contentEdgeInsets you can define a padding from the border (as mentioned in my comment above).