By Spanky


2009-09-24 02:06:31 8 Comments

How do you set the image for a UIButton in code?

I have this:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];

but don't see what will set the image for it.

11 comments

@Mike W 2009-09-24 02:16:33

Objective-C

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];

Swift 4

let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)

@leviathan 2010-07-18 16:52:54

As a sidenote: this will show the image, but the button title text will be hidden.

@user755278 2011-05-31 09:14:01

This is code is fully working but i'm getting image with parsing so how can i done this as this code is worked for me in UIImage case:- largePick.image = aNewsInfo.smallImageData; how can i done this with UIButton... Can you help me...

@Kirk Woll 2013-05-20 19:35:31

@slcott, where does the documentation state that? I don't see it suggest anywhere that setImage is deprecated. (It appears to me you are confusing UIButton.setImage with UITableViewCell.image, which is a property deprecated as you stated.)

@Apfelsaft 2014-09-16 14:41:18

Attention: If you use anything else than UIButtonTypeCustom, the image will be blue in iOS7 and 8.

@Micah Montoya 2016-12-03 03:10:54

For Swift 3: btnTwo.setImage(btnImage, for: UIControlState.normal)

@xscoder 2016-12-29 21:39:11

Swift 3 version (butt_img must be an Image Set into Assets.xcassets or Images.xcassets folder in Xcode):

btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)

Anyway, if you want the image to be scaled to fill the button's size, you may add a UIImageView over it and assign it your image:

let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)

@Leemboy 2016-11-21 03:35:24

-(void)buttonTouched:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
    {
        [btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
        // other statements....
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
        // other statements....
    }
}

@Peter Schultz 2015-12-30 23:44:03

Don't worry so much framing the button from code, you can do that on the storyboard. This worked for me, one line...more simple.

[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];

@Ajay Sharma 2010-12-09 03:54:18

You can put the image in either of the way:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self 
           action:@selector(goToOne) 
 forControlEvents:UIControlEventTouchUpInside];


[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

//OR setting as background image

[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"] 
                  forState:UIControlStateNormal];

[self.view addSubview:btnTwo];

@Mike Martin 2011-02-14 19:39:45

This part of the code has got me confused. <code> btnImage = [UIImage imageNamed:@"image.png"]; </code> What is btnImage? That wasn't in the original code. Is that a new button?

@Ajay Sharma 2011-02-14 19:39:45

you can directly set the image of the button instead of taking another image object like this: [btnTwo setImage:[UIImage imageNamed:@"image.png"]];

@Praveen-K 2011-09-21 11:08:46

you have to have buttonWithType:UIButtonTypeRoundedRect to UIButtonTypeCustom other wise button will not display as per as your image.

@jrasmusson 2012-10-19 17:24:24

Before this would work for me I had to resize the button frame explicitly based on the image frame size.

UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;

[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController 
               action:@selector(revealToggle:) 
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = 
  [[UIBarButtonItem alloc] initWithCustomView:listButton];

self.navigationItem.leftBarButtonItem = jobsButton;

@Chris Hart 2012-11-26 05:43:40

This button resizing trick is very helpful.

@bk138 2016-07-09 16:23:06

@jrasmusson just wanted to add that the 3 lines of fiddling with the frame can also be written in [listButton sizeToFit]

@Gaurav Gilani 2014-05-21 12:32:02

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"buttonimage.png"] 
               forState:UIControlStateNormal];
[btn addTarget:self 
     action:@selector(btnSendComment_pressed:) 
     forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

@Sruit A.Suk 2015-10-10 15:55:28

In case of Swift User

// case of normal image
let image1 = UIImage(named: "your_image_file_name_without_extension")!
button1.setImage(image1, forState: UIControlState.Normal) 

// in case you don't want image to change when "clicked", you can leave code below
// case of when button is clicked
let image2 = UIImage(named: "image_clicked")!
button1.setImage(image2, forState: UIControlState.Highlight) 

@Alex Cio 2015-06-15 14:32:18

I was looking for a solution to add an UIImage to my UIButton. The problem was just it displays the image bigger than needed. Just helped me with this:

_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;

Every time I want to display my UIImageView I just set the var hidden to YES or NO. There might be other solutions but I got confused so many times with this stuff and this solved it and I didn't need to deal with internal stuff UIButton is doing in background.

@Azhar 2012-11-07 10:33:55

You can do it like this

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

@leviathan 2010-06-09 11:59:55

Mike's solution will just show the image, but any title set on the button will not be visible, because you can either set the title or the image.

If you want to set both (your image and title) use the following code:

btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

@onmyway133 2013-11-19 03:52:43

what is the difference between setImage and setBackgroundImage ?

@Ernest 2013-11-24 00:37:14

setBackgroundImage will stretch the image over the width of the button over your title text while setImage will make the button an image ignoring title text without stretching the image.

Related Questions

Sponsored Content

15 Answered Questions

[SOLVED] Set UIButton title UILabel font size programmatically

33 Answered Questions

[SOLVED] How do I create a basic UIButton programmatically?

22 Answered Questions

[SOLVED] Xcode error "Could not find Developer Disk Image"

  • 2015-06-09 15:48:57
  • Nathan Kramer
  • 529431 View
  • 856 Score
  • 22 Answer
  • Tags:   ios iphone xcode

2 Answered Questions

10 Answered Questions

[SOLVED] How to set the title of UIButton as left alignment?

  • 2010-05-04 11:56:19
  • Madan Mohan
  • 185584 View
  • 426 Score
  • 10 Answer
  • Tags:   objective-c uibutton

2 Answered Questions

[SOLVED] Custom back button view

1 Answered Questions

[SOLVED] add label to UIButtons in iCarousel

2 Answered Questions

3 Answered Questions

[SOLVED] How would I compare UIButtons by their titles

  • 2011-04-07 11:47:30
  • Anand
  • 1670 View
  • 0 Score
  • 3 Answer
  • Tags:   iphone uibutton

4 Answered Questions

[SOLVED] Create elements programmatically and reuse code

Sponsored Content