简体   繁体   中英

Set background image of Button in Objective-C

I get a problem that I cannot set the background image to the UIButton. My button is a colour button and I want to have an image at the background of the button. And also, I can increase the size of the background image. My code is:

in .h

@property (weak, nonatomic) IBOutlet UIButton *color1;

in .m

UIImage *colorimage = [UIImage imageNamed:@"57_bg_selected"];
[_color1 setBackgroundImage:colorimage forState:UIControlStateNormal];

What I got is background image is step on the button.

Button should be of custom type to set an image. Verify your code once again. Set the button to custom type in Storyboard.

I can see you are using IBOutlet of a button this means you are using button from nib, then you should change background image from Attributes Inspector on right side of your xcode

otherwise if you still need it to be changed programmatically then try this

[self.color1 setBackgroundImage:[UIImage imageNamed:@"57_bg_selected.png"] forState:UIControlStateHighlighted];

Please Note you should mention extension of your image .jpg or .png in your file name

I have a button and this button has a back color.

Are you trying to set the background color as well as background image of the button. You can only apply one thing at a time.

  1. Verify if your button's type is System or Custom.

在此处输入图片说明

  1. As you are having image in interface builder, try to set the background image property in xib/storyboard.

在此处输入图片说明

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