简体   繁体   中英

IOS Resize the image in UIButton

Here is my UIButton
在此处输入图片说明
It contains a facebook icon and it's too big so I want to resize it to this. I don't want to resize the image itself because the facebook icon is using in some place with different size and in iPad the text is bigger
在此处输入图片说明
I try using the code below but it only make the image height smaller, the width don't change

 self.btnConnectWithFB.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
 self.btnConnectWithFB.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
 self.btnConnectWithFB.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);

在此处输入图片说明

What should I do to resize image for both width and height? Can I achieve it by using UIButton or I need to create a ViewGroup with UILabel and UIImageView ? Any help would be appreciated.

Simply resize the image itself . This is easily done in your code, when you create the button, by redrawing the image at smaller size into an image graphics context and extracting the resulting image.

By doing that, I was readily able to get the difference between these two buttons, both using the same original "f" image which I took directly from your screenshot:

在此处输入图片说明

Of course you can perform further tweaks if you like; a button's internal layout is highly configurable. I'm just showing you that this works. And it's trivial to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM