By user2665539

2013-08-08 18:32:08 8 Comments

On iOS 6 SDK I wrote the following lines of code to display an image inside a button:

NSURL *thumbURL2 = [NSURL URLWithString:@""];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];

But now with Xcode 5 and iOS 7 this doesn't work. The button doesn't contain the image. The button is filled with blue color.


@xscoder 2018-12-18 08:17:26

In Swift 4, initialize your UIButton and assign uyour image Data as follows:

let myButton = UIButton(type: .cutsom)
myButton.setImage(UIImage(data:myImageData), for: .normal)

@OhadM 2018-01-08 18:00:21

Using Xcode 9.2 none of the above solutions worked for what I was looking for.

I was looking for a solution that will let me set .normal and .selected UIControlState images inside the storyboard for their original rendering mode, but, inside the Swift file, no string literals should exist regarding the image names.

Basically, inside your code you will get the image you set inside your storyboard for .normal state and re-render it as .alwaysOriginal (Same for .selected state), then, you will set that image (which is now rendered as original and won't be affected by the tint) for the relevant state (.normal and .selected) of your UIButton.

Here it is:

// Get your .normal image (you set via your storyboard) and render it as original
let unselectedImage = yourButton.image(for: .normal)?.withRenderingMode(.alwaysOriginal)
// Set your normal image but this time rendered as original
yourButton.setImage(unselectedImage, for: .normal)
// Same for selected state
let selectedImage = yourButton.image(for: .selected)?.withRenderingMode(.alwaysOriginal)
yourButton.setImage(selectedImage, for: .selected)

This way you can set your button image states and if the image name will change, it won't affect your code.

@Mosa 2018-06-25 07:53:41

thank you , you really help me

@Rob Schlackman 2018-01-05 20:45:33

Old thread, but I wanted to chime in because I just had the same problem. The issue was just that you are calling setImage when you should call setBackgroundImage.

@Wilson 2017-02-28 14:42:24

Swift 3 :

let image = UIImage(named: "my-image")
myButton.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)

@Rajashekar 2015-10-23 07:19:59

making the tint color as clearcolor for all the four states(Default,Highlighted,selected,disabled) worked for me.

@Esqarrouth 2014-11-11 13:49:51

For swift:

    let aButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

@Maddy 2015-02-24 06:25:16

Thanks dude this also work's in Obj C

@BonanzaDriver 2018-03-02 03:34:09

Yep, that did the trick.

@Shane 2014-09-05 10:43:07

This Problem is called blue color problem of the button in xcode. When we make button by code the button shows the blue tint color by default.This can be solved byt assigning tint color to black or white accordingly to your cell's color. The code is :

UIImage *closebtnimg = [UIImage imageNamed:@"icon_uncheck.png"];
 UIImage *closebtnimg1 = [UIImage imageNamed:@"icon_checked.png"];

Custombutton *button = [Custombutton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(52, 66, 25, 24)];
[button setBackgroundImage:closebtnimg forState:UIControlStateNormal];
[button setBackgroundImage:closebtnimg1 forState:UIControlStateSelected];
[button setTintColor:[UIColor whiteColor]];
[cell.contentView addSubview:button];
[button addTarget:self action:@selector(changeImage:)  forControlEvents:UIControlEventTouchUpInside];

@Ricky 2014-09-05 10:44:23

its pretty helpful @Shane

@user3099609 2014-01-23 16:03:12

It seems iOS 7 is using the image provided just as an Alpha mask for displaying the button's tint color. Changing the button type to UIButtonTypeCustom did the trick for me (thanks user716216!). Setting the image as background doesn't always work if you already have a background image, as was my case.

@deadlock 2014-03-03 10:13:52

Was struggling with that for a while :D thanks dude

@jigzat 2014-03-25 18:52:50

Thank you! Original user forgot to accept it as the answer.

@chings228 2014-04-04 11:24:47

it's working for me , but change to system not work for my case , thanks all of your contribution

@datha 2013-12-09 12:41:32

This worked for me

[myButton1 setBackgroundImage:[UIImage imageNamed:@"phones.png"] forState:UIControlStateNormal];

Note:Remove front image before doing this. 2014-07-10 06:16:33

how to remove background image please!!

@DrDisc 2013-11-21 16:33:31

The issue is the TintColor. By default, iOS throws a blue tint color over every button. You can get around it through 3 ways.

  1. Change the tint color. [button setTintColor:[UIColor blackColor]]; This may color your image in ways you don't want it to.

  2. As most other suggested, set the background image. [button setBackgroundImage:[UIImage...]];

  3. Add an UIImageView to your button.

UIImageView * img = [[UIImageView alloc] initWithImage:[UIImage...]];

[button addSubView:img];

@Chamira Fernando 2013-08-12 12:30:50

In iOS7 there is new button type called UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button

Check your .xib file and change button type to Custom Look at the image

To do this programmatically, add this line to the viewDidLoad:

[UIButton buttonWithType:UIButtonTypeSystem]; 

@djskinner 2013-10-01 16:04:39

How can this be done in code?

@avuthless 2013-10-14 13:02:45

[UIButton buttonWithType:UIButtonTypeSystem];

@JWK 2013-12-18 22:52:32

Note that this solution does remove the system tint color, but also results in a hard transition between the highlighted and normal states. If you're looking to preserve the system behavior, animations, etc., but want to get rid of the tint color, try [myUIButton setImage: [[UIImage imageNamed: @"myButtonImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] forState: UIControlStateNormal]; (with the button type set to "System" in IB).

@g_pass 2014-05-22 00:16:38

Thanks this was very helpful. In our case, the button was even clipping to the shape of the transparent PNG and then tinting.

@Maury Markowitz 2014-09-04 22:03:11

Geezm they hid that one well didn't they?!

@Nico 2014-10-22 10:07:21

It's not working for iOS 8.0...!!!!!

@Chamira Fernando 2014-10-22 10:34:25

it works :) u might have some other problem in ur code. If you look at Xcode 6.1 you can clearly see the available options..

@CupawnTae 2015-12-20 17:55:47

@ChamiraFernando I know it's more than two years on, but could you check the approved edit at the end of your answer - shouldn't it be UIButtonTypeCustom? Edit should really have been rejected anyway, even if it had been correct, as it should have been posted as a comment instead.

@CupawnTae 2015-12-20 18:08:34

(not to mention the fact that just adding the line in question won't do anything useful at all)

@Chris Ho 2016-04-13 02:57:00

in swift : UIButton(type: UIButtonType.Custom), thank you!

@Alex 2013-10-14 15:27:50

None of the given solutions were working for me. If you do not set an initial image in Storyboard, you can still change the image of the button by using setBackgroundImage.

For your example, only a minor change is needed.

NSURL *thumbURL2 = [NSURL URLWithString:@""];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setBackgroundImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];

@Ryan Heitner 2013-09-23 07:56:13

I had the same issue. On my storyboard I had a button without any image.

I would then assign the image in the code.

IOS 7 came and I got a lot of blue images.

The resolution was simple yet confusing. If I assign any image on the storyboard and then change the image at run time it works fine.

You always must specify a starting image on the storyboard even if you are not going to use it.

@CodyMace 2017-07-21 05:04:53

This is what worked for me

@Mick MacCallum 2013-08-08 18:44:12

There's a good chance that the image is there and you just can't see it. Try changing the button's type to UIButtonTypeCustom. If that doesn't work, set the button's background color to [UIColor clearColor];

Related Questions

Sponsored Content

15 Answered Questions

[SOLVED] Color Tint UIButton Image

34 Answered Questions

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

34 Answered Questions

5 Answered Questions

[SOLVED] UIButton remove all target-actions

  • 2010-07-27 04:50:14
  • SK9
  • 69373 View
  • 321 Score
  • 5 Answer
  • Tags:   ios uibutton

13 Answered Questions

[SOLVED] How to round the corners of a button

15 Answered Questions

[SOLVED] Set UIButton title UILabel font size programmatically

11 Answered Questions

[SOLVED] Setting an image for a UIButton in code

10 Answered Questions

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

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

1 Answered Questions

[SOLVED] Swift: Add image to CAShapeLayer

Sponsored Content