1

I'm trying to add text to the bottom of my image but the setImage is hiding my text.

Top1.setImage(UIImage(named: "DefaultPic.jpg"), forState: UIControlState.Normal)
Top1.setTitle("#1", forState: UIControlState.Normal)
Top1.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
Top1.titleEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);

I can't use background image because I want the text and image to have a minor gap between them without having to use a seperate UILabel

  • http://stackoverflow.com/questions/11717219/uibutton-image-text-ios – Piyush Aug 31 '15 at 20:10
  • http://stackoverflow.com/questions/4564621/aligning-text-and-image-on-uibutton-with-imageedgeinsets-and-titleedgeinsets – Piyush Aug 31 '15 at 20:11
  • @PiyushPatel I've looked at both of those examples earlier, they both still hide my text in ios8. If I use background image then I wont be able to use inset on the image to create the gap I want –  Aug 31 '15 at 20:14
  • try this .. Top1.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 5); Top1.contentEdgeInsets =UIEdgeInsetsMake(0, 30, 0, 0); – Piyush Aug 31 '15 at 20:20
  • @PiyushPatel That pushes the image to the right and the text is still hidden –  Aug 31 '15 at 20:27
  • so you button has height 30? and storyboard doesn't have any contentEdgeInset added? – Chameleon Aug 31 '15 at 20:32
  • @Dustin My Button has a width of 49 and height of 51 –  Aug 31 '15 at 20:33
  • so your code moves image button bottom up only 10 and title top down 20...meaning still 21 points of overlap – Chameleon Aug 31 '15 at 20:34

1 Answers1

0
Top1.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
Top1.titleEdgeInsets = UIEdgeInsetsMake(31, 0, 0, 0);

if you want title under image

Chameleon
  • 1,608
  • 3
  • 21
  • 39
  • Yes, I want the title under the image. But this code still has my title hidden. –  Aug 31 '15 at 20:36
  • I'm trying view debugger...seems the title is empty. The `UIEdgeInsetsMake` is working properly for both image and title. I placed code to setImage(nil) this causes title to appear where desired...I don't understand why when Image is set title is hidden (meaning not visible, although properly offset) – Chameleon Aug 31 '15 at 21:08