Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with the imageView set to UIViewContentModeScaleAspectFill doesn't fill completely with small images

I have a UIButton in my app, where instead of text I give it an image, as it servers as a thumbnail button that will present the full image upon being tapped.

I saw this answer as seemingly the accepted implementation of this, but with images smaller than the button themselves, the image view doesn't actually fill the button as the contentMode should, or as it would with an image view, as shown below:

enter image description here

As you can see, with the image view it expands properly, but with the button it doesn't. (The button background is set to blue so you can see where it's not showing up.)

The code I'm doing is dead simple:

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

How do I change it so that it fills it properly?

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

like image 262
Doug Smith Avatar asked Jan 09 '23 07:01

Doug Smith


1 Answers

Setting contentHorizontalAlignment and contentVerticalAlignment to .Fill on the UIButton makes it extend fully.

like image 77
Doug Smith Avatar answered Jan 21 '23 02:01

Doug Smith