skip to Main Content

I am trying to get an image on top of text on a UIButton.
I have put the image as the background image on the UIButton
and set the VerticalAlignment and the image now does sit above the text 🙂
like this :

button.imageView?.image
button.setBackgroundImage(image, forState: UIControlState.Normal)
button.titleLabel?.font = UIFont(name: "Cantarell", size: 14)
button.setTitle("Test", forState: UIControlState.Normal)
button.contentVerticalAlignment = UIControlContentVerticalAlignment.Bottom

The only problem is that the image now fills the whole button up and it looks rubbish and blocky. I have tried to crop the image with Photoshop and it still fills the button and looks blocky. Can I set the backgroundImage frame size for example in swift ?

So the image ends up like this :

enter image description here

So the image needs to be smaller and its blocky. I have cropped it in Photoshop and it doesn’t help. Just makes it more blocky. Is there .frame I can set for the size of the button background image ?

2

Answers


  1. This isn’t a solution via programming per say, but I believe if you open the image in photoshop and increase the size of the canvas, adding more blue around the outside, then the white part of the image will take up less space in the button and appear less blocky.

    For this to work you’ll have to make sure the image view’s mode is set to either aspect fit or aspect fill, I believe.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search