A UIButton has two types of images that it can display - a foreground image and a background image. The background image for the button is supposed to replace the button's background texture. Thus, it will stretch to fill the entire background. It is assumed that the foreground image of the button will be an icon that may or may not be displayed next to text; he does not stretch. It may shrink if the frame is smaller than the image, but it will not stretch.
The foreground button and background image can be set in the code as follows:
// stretchy [self setBackgroundImage:backgroundImage forState:UIControlStateNormal]; // not stretchy [self setImage:forgroundImage forState:UIControlStateNormal];
By default, the button's backgroundImage button will use scaleToFill to stretch the image. If you want the image to be stretched using attachments on the cover, you must set them on the image before assigning it backgroundImage, for example:
UIImage *image = [UIImage imageNamed:@"bg_image.png"]; image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)]; [self setBackgroundImage:image forState:UIControlStateNormal];
source share