1

I have a button which is set at 62,62 square. I have an image which is 40,40 square.

I want the image in the button (which is easy and done), but I want it at the top of the button frame and I want some text at the bottom. So it looks like this :

enter image description here

Any idea how to do this in swift ?

I have got so far :

button.backgroundColor = UIColor(red: 0.13, green: 0.48, blue: 0.81, alpha: 1.0)
button.tag = i
button.imageView?.image
button.setImage(image, forState: .Normal)
button.setTitle("Test", forState: UIControlState.Normal)

The test setTitle, comes to the right of the image as ... form. Anyone can advise ?

Jason
  • 1,057
  • 3
  • 13
  • 31
  • `imageEdgeInsets` and `titleEdgeInstets` properties of `UIButton` seems to be what you're looking for. Another topic about it: http://stackoverflow.com/questions/4564621/aligning-text-and-image-on-uibutton-with-imageedgeinsets-and-titleedgeinsets (which could be I think easily translated in Swift) – Larme Jan 19 '15 at 15:12
  • Cheers Larme, will have a look! – Jason Jan 19 '15 at 15:15

1 Answers1

0

Using the interface builder;

  • set the image to the Background property of the button. Using code it is button.setBackgroundImage

  • from Control section set the horizontal alignment of the text to Bottom. Using code it is button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentBottom

Erol
  • 91
  • 2
  • 10