Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - UIButton with non resized image inside

I have a UIButton with a "custom" type, with an "image" property that gets its image from resources. The image is smaller than the button. I cannot achieve into Interface Builder to keep the original size of the image while scaling the button's dimensions as I want.

How may I have a button of any size into IB, keeping the original image size, aligning it on center, left right, bottom, inot the button... ? Isn't it possible without putting a UIImageView on it ?

like image 725
Oliver Avatar asked Apr 11 '11 18:04

Oliver


2 Answers

Make sure that the contentHorizontalAlignment and contentVerticalAlignment properties of your button are not set to UIControlHorizontalAlignmentFill and UIControlVerticalAlignmentFill respectively. Those values will scale the button image to fill the button frame. Any other value will align the image appropriately.

like image 146
XJones Avatar answered Oct 22 '22 21:10

XJones


Use image foreground to put non-resizable image elements, found in a post on stackoverflow but forget where it is.

button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0,-10,0,0);

[button setImage:[UIImage imageNamed:@"back-button"] forState:UIControlStateNormal];
like image 4
freezing_ Avatar answered Oct 22 '22 22:10

freezing_