1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Dara Tith
  • 518
  • 7
  • 21
  • what do you mean your image is a color button? Try setting the image, instead of setting the background image? – Siriss Aug 25 '15 at 02:32
  • 2
    _color1 instance is not nil ? – Chope Aug 25 '15 at 02:33
  • my button is a round button which it has a color. what i got, my background image is in that button's size. – Dara Tith Aug 25 '15 at 02:34
  • Sorry, but you're not being clear. – Hot Licks Aug 25 '15 at 02:35
  • Yes, i will explain you. I have a button and this button has a back color. so, we can see that button by its color is black. but i want to set the background button, mean i want to have an image at the back of that button. but i got an image that i want to set is in the button's size. – Dara Tith Aug 25 '15 at 02:38
  • can you please add the code where you create the button? – Siriss Aug 25 '15 at 02:39
  • possible duplicate of [Programmatically set image and text on UIButton](http://stackoverflow.com/questions/9887959/programmatically-set-image-and-text-on-uibutton) – Adrian Aug 25 '15 at 02:45

3 Answers3

1

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

Rohit Kumar
  • 877
  • 6
  • 20
0

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

Irfan Anwar
  • 1,878
  • 17
  • 30
0

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.

enter image description here

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

enter image description here

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35