UIButton query in swift IOS

2019-08-01 16:32发布

问题:

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 :

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 ?

回答1:

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