Fahem Issameddine
Fahem Issameddine

Reputation: 393

UIButton background image stretched Xcode iOS

I have UIButton that was created with storyboard. The size of the button is different on different iPhone devices. Then I set as background image an image with same width and height (images assets) for different resolution. I test my app with iPhone 6 and iPhone 4. The size of my button is different on both screens. That is normal, but the background image is stretched on iPhone 4 (with iPhone 6 not because probably the size of the button on iPhone 6 is square (same width and height)).

My question is if it is possible to show the image without stretching no matter the button size. I searched on the net and found about slicing image. But I don´t understand how can I solve this problem?

Upvotes: 1

Views: 819

Answers (1)

Sujay
Sujay

Reputation: 2520

Background Image and Image for UIButton, both are having different functionality. If you set background Image for a UIButton, it will stretch your image to fit but If you set image for UIButton, then it won't stretch your image So you can try to set image for button instead of background image

To set image for UIButton

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btn setImage:btnImage forState:UIControlStateNormal];

To set background image for UIButton

[btn setBackgroundImage:btnImage forState:UIControlStateNormal];

Upvotes: 1

Related Questions