16

I am a newbie with Objective C. How would I set the background of this button to an image (image is already in the resources folder in XCode, "blue_button.png") instead of clearColor? Also, I would prefer that I use the image as the shape of the button rather than the UIButtonTypeRoundedRect.

btnClear = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

btnClear.frame = CGRectMake(115, 350, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

btnClear.backgroundColor = [UIColor clearColor];

[btnClear addTarget:self action:@selector(clearAction:) 

forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

I know how to do this in Interface Builder but I would rather learn about doing it in XCode.

squ1dd13
  • 84
  • 2
  • 9
Linda
  • 1,973
  • 4
  • 21
  • 24

7 Answers7

44

This works:

UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btnClear.frame = CGRectMake(115, 200, 90, 40);
[btnClear setTitle:@"Clear" forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClear];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Linda
  • 1,973
  • 4
  • 21
  • 24
8

See the UIButton class reference: -setBackgroundImage:forState:

Create your button with type UIButtonTypeCustom instead of UIButtonTypeRoundedRect if you don't want to use the rounded corner style.

Jonah
  • 17,918
  • 1
  • 43
  • 70
6

You need to declare your button with the following UIButtonTypeCustom type:

btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

Then you should be able to use the follow:

[btnClear setImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];

There are 6 different control states that can be set.

enum {
   UIControlStateNormal               = 0,
   UIControlStateHighlighted          = 1 << 0,
   UIControlStateDisabled             = 1 << 1,
   UIControlStateSelected             = 1 << 2,
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
};

Here are some references that may help:

UIButton Class Reference

UIControl Class Reference

Larry Hipp
  • 6,205
  • 3
  • 26
  • 31
  • This does work but I am now unsure how to set the title. I was using [btnClear setTitle:@"Clear" forState:UIControlStateNormal]; – Linda Jan 05 '11 at 21:16
  • have a look at this and see if it helps. http://stackoverflow.com/questions/1469474/setting-an-image-for-a-uibutton-in-code – Larry Hipp Jan 05 '11 at 21:45
  • You can either set and image or a tile but there are some things you can do to get the title to show – Larry Hipp Jan 05 '11 at 21:45
3

You can use [UIColor colorWithPatternImage:] to set background color to image. To use image you should change button style to custom.

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
2

Swift 4+

var btnClear = UIButton()
btnClear = UIButton(type: .custom)
btnClear.frame = CGRect(x: 115, y: 200, width: 90, height: 40)
btnClear.setBackgroundImage(UIImage(named: "blue_button.png"), for: .normal)
view.addSubview(btnClear)
1

No need to set button to UIButtonTypeCustom - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state method also works for UIButtonTypeRoundedRect

Adrian P
  • 6,479
  • 4
  • 38
  • 55
phani
  • 11
  • 1
1

if you want to set button background with images on the Internet, using SDWebImage is an elegant choice:

#import <SDWebImage/UIButton+WebCache.h>

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
[button setBackgroundImageWithURL:imageURL forState:UIControlStateNormal];
Brian
  • 30,156
  • 15
  • 86
  • 87