Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scale a UIButton's imageView?

I created a UIButton instance named "button" with an image using [UIButton setImage:forState:]. The button.frame is larger than the image's size.

Now I want to scale this button's image smaller. I tried changing button.imageView.frame, button.imageView.bounds and button.imageView.contentMode, but all seem ineffective.

Can anyone help me scale a UIButton's imageView?

I created the UIButton like this:

UIButton *button = [[UIButton alloc] init]; [button setImage:image forState:UIControlStateNormal]; 

I tried to scale the image like this:

button.imageView.contentMode = UIViewContentModeScaleAspectFit; button.imageView.bounds = CGRectMake(0, 0, 70, 70); 

and this:

button.imageView.contentMode = UIViewContentModeScaleAspectFit; button.imageView.frame = CGRectMake(0, 0, 70, 70); 
like image 734
vcLwei Avatar asked Dec 24 '09 08:12

vcLwei


People also ask

How do I scale a button image in Swift?

Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.


1 Answers

For the original poster, here is the solution I found:

commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; 

This will allow your button to scale horizontally. There is a vertical setting as well.

Took me several hours to figure that one out (the naming of the property is very unintuitive) so figured I'd share.

like image 175
Chris Avatar answered Sep 30 '22 14:09

Chris