Earl Grey Earl Grey - 2 years ago 114
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download