Setting UIButton image results in blue button in iOS 7

UibuttonUiimageIos7

Uibutton Problem Overview


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

NSURL *thumbURL2 = [NSURL URLWithString:@"http://example.com/thumbs/2.jpg"];
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.

Uibutton Solutions


Solution 1 - Uibutton

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

Solution 2 - Uibutton

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.

Solution 3 - Uibutton

Swift 3, 4, 5 :

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

Solution 4 - Uibutton

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

Solution 5 - Uibutton

For swift:

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

Solution 6 - Uibutton

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

Solution 7 - Uibutton

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.

Solution 8 - Uibutton

This worked for me

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

Note:Remove front image before doing this.

Solution 9 - Uibutton

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.

Solution 10 - Uibutton

In iOS 13 -- just set the Tint property to White, while keeping the type of the UIButton as Custom

Solution 11 - Uibutton

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:@"http://example.com/thumbs/2.jpg"];
NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
UIImage *thumb2 = [UIImage imageWithData:thumbData2];
[btn2 setBackgroundImage:thumb2 forState:UIControlStateNormal];
[self.view addSubview:btn2];

Solution 12 - Uibutton

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

Solution 13 - Uibutton

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.

Solution 14 - Uibutton

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

Solution 15 - Uibutton

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2665539View Question on Stackoverflow
Solution 1 - UibuttonChamira FernandoView Answer on Stackoverflow
Solution 2 - Uibuttonuser3099609View Answer on Stackoverflow
Solution 3 - UibuttonWilsonView Answer on Stackoverflow
Solution 4 - UibuttonMick MacCallumView Answer on Stackoverflow
Solution 5 - UibuttonEsqarrouthView Answer on Stackoverflow
Solution 6 - UibuttonDrDiscView Answer on Stackoverflow
Solution 7 - UibuttonRyan HeitnerView Answer on Stackoverflow
Solution 8 - UibuttondathaView Answer on Stackoverflow
Solution 9 - UibuttonRob SchlackmanView Answer on Stackoverflow
Solution 10 - UibuttonrommexView Answer on Stackoverflow
Solution 11 - UibuttonAlexView Answer on Stackoverflow
Solution 12 - UibuttonShaneView Answer on Stackoverflow
Solution 13 - UibuttonOhadMView Answer on Stackoverflow
Solution 14 - UibuttonRajashekarView Answer on Stackoverflow
Solution 15 - UibuttonFrank EnoView Answer on Stackoverflow