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.

7
  • what do you mean your image is a color button? Try setting the image, instead of setting the background image? Commented Aug 25, 2015 at 2:32
  • 2
    _color1 instance is not nil ? Commented Aug 25, 2015 at 2:33
  • my button is a round button which it has a color. what i got, my background image is in that button's size. Commented Aug 25, 2015 at 2:34
  • Sorry, but you're not being clear. Commented Aug 25, 2015 at 2: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. Commented Aug 25, 2015 at 2:38

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, now i can change it. ;)
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

1 Comment

that's ok if i didn't mention extension.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.