Setting an image for a UIButton in code

306,869

Solution 1

Objective-C

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

Swift 5.1

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

Solution 2

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];

Solution 3

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;

Solution 4

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) 

Solution 5

You can do it like this

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
Share:
306,869

Related videos on Youtube

Spanky
Author by

Spanky

Updated on October 07, 2021

Comments

  • Spanky
    Spanky over 2 years

    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.

  • leviathan
    leviathan almost 14 years
    As a sidenote: this will show the image, but the button title text will be hidden.
  • Mike Martin
    Mike Martin about 13 years
    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
    Ajay Sharma about 13 years
    you can directly set the image of the button instead of taking another image object like this: [btnTwo setImage:[UIImage imageNamed:@"image.png"]];
  • user755278
    user755278 almost 13 years
    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...
  • Praveen-K
    Praveen-K over 12 years
    you have to have buttonWithType:UIButtonTypeRoundedRect to UIButtonTypeCustom other wise button will not display as per as your image.
  • Kirk Woll
    Kirk Woll almost 11 years
    @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.)
  • onmyway133
    onmyway133 over 10 years
    what is the difference between setImage and setBackgroundImage ?
  • Ernest
    Ernest over 10 years
    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.
  • Apfelsaft
    Apfelsaft over 9 years
    Attention: If you use anything else than UIButtonTypeCustom, the image will be blue in iOS7 and 8.
  • bk138
    bk138 almost 8 years
    @jrasmusson just wanted to add that the 3 lines of fiddling with the frame can also be written in [listButton sizeToFit]
  • Micah Montoya
    Micah Montoya over 7 years
    For Swift 3: btnTwo.setImage(btnImage, for: UIControlState.normal)
  • Mike Irving
    Mike Irving about 5 years
    or even simpler, the second line in Swift btnTwo.setImage(btnImage, for: .normal)