4

I have customized UIButton with the following properties:

  1. 84x44 frame
  2. 84x44 background image
  3. 32*32 image that should be centered

but my image is always aligned to the left. How can I align it back to center position on the UIButton?

I have a title on my button that is hidden (clearColor). These titles are key_value in a dictionary so I know which button is pressed and return corresponding value in the dictionary. I now understand that it's these text that is pushing my image sideway, but how do I fix that?

Under attribute inspector, there is a tap named Edge, set to Image. I set left value as 22 and all image align to center. Can someone explain to me how this works?

Pang
  • 9,564
  • 146
  • 81
  • 122
Shakesbeer
  • 513
  • 1
  • 7
  • 12
  • Maybe a repeated question : http://stackoverflow.com/questions/4564621/aligning-text-and-image-on-uibutton-with-imageedgeinsets-and-titleedgeinsets – Ashraf Jan 26 '12 at 06:21
  • I do have text (title) on my button, but they are suppose to be hidden for identification purpose – Shakesbeer Jan 26 '12 at 06:30

1 Answers1

21

Check that your titleLabel is blank - if it has text, it will push the image to the left to make room for the titleLabel on the right.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Absolutely this. Strange and unintuitive that adding an Image to the UIButton changes my (previously Blue) text to White. White text was hidden on the White button, so I wasn't able to see the text that was pushing the image out of center. – pkamb Sep 06 '16 at 23:58
  • 2
    Thanks. Worked for me too. But one extra piece of advice for the new Xcoders: you have to backspace out the title AND hit return. I wondered why this didn't work the 1st time - that was why. – user3741598 Dec 16 '17 at 20:41