UIButton with image set to UIViewContentModeScaleAspectFill not completely filled with small images

I have UIButton

in my application where instead of text I give it an image as it is a server as a thumbnail button that will display the full image when listening.

I saw this answer as a seemingly accepted implementation of this, but with images smaller than the button itself, the image does not fill the button in fact, the contentMode should, or as it would be with the image view as shown below:

enter image description here

As you can see, with the image rendered correctly, but with the button, no. (The background of the button is set to blue so you can see where it doesn't appear.)

The code I am doing is dead simply:

myButton.setImage(UIImage(named: "pokemon"), forState: .Normal)
myButton.imageView!.contentMode = .ScaleAspectFill

      

How can I change it so that it fills it in correctly?

Sample project: http://cl.ly/0z2T1A1Z3v3m

+3


source to share


2 answers


Setting up contentHorizontalAlignment

and contentVerticalAlignment

on .Fill

to UIButton

making it fully stretched.



+5


source


Set an image as the background of your button. This will fill the border of the button and there is no need to change the content mode. Set the image as the background of the button using the method setBackgroundImage()

. Good luck!

EDIT

Option 1 :

How about using UIImageView

with UITapGestureRecognizer

and desired contentMode

in the image view?



Option 2 :

Crop the image to the size of the button and set it as the background image of the button.

Hope this helps!

+1


source







All Articles