buttons turn blue when backgroundimage is set - ios

I have a few buttons and when I set a picture as background the icons turn blue, for example:
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
All pictures are black and white, so I don't understand why they change their color when I set them as a backgroundimage.

Make sure to init the button as a "UIButtonTypeCustom"
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Then if you want the whole button to be an image, you use:
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
... Or if you want to have the image as the background, and still see the "title", you use:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];

First do
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
and then set image
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];

Related

Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?

[myButton setImage:[UIImage imageNamed:#"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;
I have tried this code and it is not working.
You should use the setBackgroundImage:forState. The setImage:forState changes the image which is on the same depth level as the button title. Additionally the backgroundImage is automatically fit to the button's view bounds. The image is not.
Implement onTouchDown action of that button and set button background image in that function by [btn setBackgroundImage: forState:]

UIButton background image not changing on tap

I've got an UIButton which I'm successfully setting the background image for. I want to change the background image when the user taps it (which navigates to the next view controller). Unfortunately, the background doesn't change for taps. However, it does change when I tap and hold the button so I'm pretty confused. Code looks like the following:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x_offset, y_offset, width, BUTTON_HEIGHT)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
UIImage *buttonImage = [[UIImage imageNamed:#"main_button"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
UIImage *pressedbuttonImage = [[UIImage imageNamed:#"main_button_pressed"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateHighlighted];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateApplication];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateDisabled];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateReserved];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateSelected];
return button;
I know I overdid it with the UIControlStates but I wanted to demo that I've tried them all :)
The issue is you are only changing the images when each event occurs. When you tap and hold it'll show the highlighted image. When you change that state it revert back to normal state and show the image (Normal background image)
You can achieve the desired behavior in two ways:
1)
In your IBAction, change the normal button image.
- (IBAction)buttonPressed:(UIButton *)button
{
UIImage *pressedbuttonImage = [[UIImage imageNamed:#"main_button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateNormal];
}
2)
Set selected or highlighted property of button to true
- (IBAction)buttonPressed:(UIButton *)button
{
button.selected = YES;
}
Well the issue is once you have stopped tapping the button, the button's background image is going to default to whatever background image you set for the UIControlStateNormal. So what you need to do is when your button is tapped, reset the UIControlStateNormal background image to whatever image you want after selection of the button.

Why image is strectching

I have UIbutton with image. I created small image using photoshop with png format. But when it displays in mobile i'm getting stretching. This image is stretching for iphone4 & iphone5 screen sizes. But navigation bar back button text is not stretching. See the difference between back and search button text.
searchbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//searchbtn = [UIButton buttonWithType:UIButtonTypeCustom];
searchbtn addTarget:self action:#selector(search:) forControlEvents:UIControlEventTouchUpInside];
//[searchbtn setImage:[UIImage imageNamed:#"searchinside_iphone.png"] forState:UIControlStateNormal];
[searchbtn setImage:[UIImage imageNamed:#"searchinside_iphone.png"] forState:UIControlStateNormal];
[searchbtn setTitle:#"Search" forState:UIControlStateNormal];
searchbtn.frame = CGRectMake(200, 5, 140/2, 60/2);

How to make an UIButton with emboss background image?

This is the effect of button on iPad Safari:
Now I have only the icon file like that:
How can I programmatically make an UIButton with the icon image as background look like the Safari on iPad?
Thank you very much!
UIImage *buttonImage = [UIImage imageNamed:#"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
try this one..
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 150, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"bg.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"ar.png"] forState:UIControlStateNormal];
bg.png
ar.png

UIButton label not visible

I'm making a custom UIButton with 2 images, but for some reason the title is just not showing up, the button is showing fine, but without any title.
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(10, 250, 300, 43);
[_button addTarget:self action:#selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
[_button setTitle:#"Login" forState:UIControlStateNormal];
[_button setTitle:#"Login" forState:UIControlStateHighlighted];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.view addSubview:_button];
These two lines:
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
should be
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
Explanation: if a foreground image is set for UIButton, the image is placed above the title, so it might hide it. If you want an image and a title to be displayed, you have to set a background image.
When you set the image, that overrides the title. If you can make your image a background image, that will work. Otherwise you will need to create a composite image for the button by essentially creating a new image which is your desired image with the title drawn over it.

Resources