2

I am setting two UIButtons programmatically which look like this:

enter image description here

I am trying to set the image and text insets to be the same in buttons, but somehow left button's image is located differently than the right one. I have this extension to UIButton class:

func setTextAndImageForButton(spacing: CGFloat) {
        let insetAmount = spacing / 2
        imageEdgeInsets = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: insetAmount)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: -insetAmount)
        contentEdgeInsets = UIEdgeInsets(top: insetAmount, left: 0, bottom: insetAmount, right: insetAmount*1.5)
    }

As far as I understand when I set the insets for image, text and whole content, it should be located in the same place for both instances.

Are the insets override somehow after I set them in code?

Bastek
  • 899
  • 1
  • 12
  • 27
  • May be image inset is changed with your title length. – Nirav D Nov 25 '16 at 11:05
  • [contentHorizontalAlignment](https://developer.apple.com/reference/uikit/uicontrol/1618213-contenthorizontalalignment) – Desdenova Nov 25 '16 at 11:05
  • OK, when I set 'contentHorizontalAlignment = .left' it is moved to the left as expected. In documentation there is written that "The default value of this property is left." so I am wondering why it was centered by default. Is there a way to have UIButton's icon aligned to left and in the same time UIButton's title centered without making new class for it? – Bastek Nov 25 '16 at 12:39
  • Share your screenshort. – Rajni Johar Raswant Nov 25 '16 at 12:39
  • You can try how insets behave for different settings using this app https://github.com/tomas789/UIButtonEdgeInsets – tomas789 Jan 23 '17 at 09:09

0 Answers0