Jenny Cheung
Jenny Cheung

Reputation: 301

When changing the button image, the button size is changed. Why?

In IB, the button size and location have been set perfectly well using autoLayout. But when I try to change the button image using the following

if (need_Update) {
[_updateButton setImage:[UIImage imageNamed:@"ipad_012_01_update2.png"] forState:UIControlStateNormal];
} 
else {
[_updateButton setImage:[UIImage imageNamed:@"ipad_012_01_update.png"] forState:UIControlStateNormal];
}

the button size has been changed. I wonder why would that happen? And how could I fix it?

I mean, for example, the original button image is iPad_012_01_update.png, and it displays perfectly. When the need_Update value is YES, and the button image is changed to ipad_012_01_update2.png, but the button size is also changed.

Thanks in advance!

Summary

Using Background Image is a very good way to prevent unwanted size changing!

Also, there's a lesson: If you do want to use button image instead of button background image, be sure that the two button images are of the same resolution! This will reduce the chance that the image is resized itself.

Upvotes: 3

Views: 124

Answers (3)

Vikas Dadheech
Vikas Dadheech

Reputation: 1720

From the looks of it, it seems like your image is extending beyond the bounds of the button, so to you it looks like the button size is changing.

To test this, try setting the clipsToBounds to YES for the button.

It will clip anything that is going beyond the bounds of button.

So you will be able to analyze it in a better way and decide accordingly about how you want to move forward.

Upvotes: 0

iPhone
iPhone

Reputation: 4170

Try following

if (need_Update) {
[_updateButton setBackgroundImage:[UIImage imageNamed:@"ipad_012_01_update2.png"] forState:UIControlStateNormal];
} 
else {
[_updateButton setBackgroundImage:[UIImage imageNamed:@"ipad_012_01_update.png"] forState:UIControlStateNormal];
}

Hope this helps

Upvotes: 2

Annie
Annie

Reputation: 147

The background image is scaled to fill the bounds of the UIButton, and is displayed behind the title. The foreground image is not scaled, and displayed next to the button title. So try to set the backgroundImage of a button.

UIButton image and background image, which one to use for custom image?

Upvotes: 0

Related Questions