简体   繁体   中英

Custom UIButton image iOS

Is it possible to have a custom UIButton that has an image covering only half of it? I imagine it would be something like this:

UIButton *someButton = [UIButton buttonWithType:UIButtonTypeCustom];
[someButton setBackgroundImage:[UIImage imageNamed:@"buttonPicture"]];

/* Then there must be some property I can set that prevents the button from stretching the image?? */

.
.
.

I want to have a 50x100px button which only has a 50x50px image on the top half, and to be transparent on the bottom half.

I know how to create a custom button and everything. Im just woondering what the property is that controls the stretching of the backgroundImage.

Thanks!

Use

[someButton setImage:[UIImage imageNamed:@"buttonPicture"] forState: UIControlStateNormal];
 someButton.imageEdgeInsets = UIEdgeInsetsMake(-50, 0, 0, 0);

I think you could:

  1. create your button image as a 50x100px image (same size as the button);

  2. make this image be half transparent (PNG with alpha),

instead of trying to assign the button a smaller image covering only half of it and prevent stretching.

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