UIButton keep content on top of backgroundImage - ios

I have a UIButton and I'm setting a background image on it:
[self.button setBackgroundImage:[UIImage imageNamed:#"button-background"] forState:UIControlStateNormal];
I want the content of the button to stay on top of the background image, and not be affected by it. So when the button content is text, the font color of the text would be unaffected by changing the background image. I'd like it to behave similarly when the button content is an image.
What's the best way to achieve this behavior?

Setting the background of the button does exactly what you're asking. However, make sure your button is in the state you're setting the background image for. Text will appear over this image based on your settings.
forState:UIControlStateNormal (Highlighted, selected, etc)

Related

Tint custom image in UIBarButton Item

To animate the image in a UIBarButton Item, I've created two regular UIButtons, button1 and button2 with different images, image1 and image2 and assign them to the UIBarButtonItem's customView property with:
self.myBarButton.customView = button1;
I am able to do the animation by assigning one or the other buttons to the UIBarButtonItem.
My problem is the first image is a line drawing that I want to tint. The second is a full color image that I don't want to tint. For some reason, if I set the rendering mode of the first image to UIImageRenderingModeTemplate which allows it to tint, then the second image does not display even if I set the rendering mode of the second to UIImageRenderingModeAlways.
Is there any way to tint the image that does not involve changing the rendering mode. I do have the line [button1 setTintColor:[UIColor blueColor]]; but it has no effect.
Also can anyone explain why setting the rendering mode would prevent the second image from appearing?
Thanks in advance for any suggestions.

When changing the button image, the button size is changed. Why?

In IB, the button size and location have been set perfectly well using autoLayout. But when I try to change the button image using the following
if (need_Update) {
[_updateButton setImage:[UIImage imageNamed:#"ipad_012_01_update2.png"] forState:UIControlStateNormal];
}
else {
[_updateButton setImage:[UIImage imageNamed:#"ipad_012_01_update.png"] forState:UIControlStateNormal];
}
the button size has been changed. I wonder why would that happen? And how could I fix it?
I mean, for example, the original button image is iPad_012_01_update.png, and it displays perfectly. When the need_Update value is YES, and the button image is changed to ipad_012_01_update2.png, but the button size is also changed.
Thanks in advance!
Summary
Using Background Image is a very good way to prevent unwanted size changing!
Also, there's a lesson: If you do want to use button image instead of button background image, be sure that the two button images are of the same resolution! This will reduce the chance that the image is resized itself.
Try following
if (need_Update) {
[_updateButton setBackgroundImage:[UIImage imageNamed:#"ipad_012_01_update2.png"] forState:UIControlStateNormal];
}
else {
[_updateButton setBackgroundImage:[UIImage imageNamed:#"ipad_012_01_update.png"] forState:UIControlStateNormal];
}
Hope this helps
The background image is scaled to fill the bounds of the UIButton, and is displayed behind the title. The foreground image is not scaled, and displayed next to the button title. So try to set the backgroundImage of a button.
UIButton image and background image, which one to use for custom image?
From the looks of it, it seems like your image is extending beyond the bounds of the button, so to you it looks like the button size is changing.
To test this, try setting the clipsToBounds to YES for the button.
It will clip anything that is going beyond the bounds of button.
So you will be able to analyze it in a better way and decide accordingly about how you want to move forward.

UIButton image opacity required

I created a button on interface builder and set set its "image" to a png file and did not set any background image to it.
I want the button background to be transparent so I set its alpha to 0.6 in IB. The problem is I DO NOT WANT the image to be transparent too ( only the surrounding background ) but the image apparently inherits the button alpha and I can see throughout it.
Is there a way to completely avoid the button image to be transparent ?
Thanks
Update 1:
Using Xcode 5.02
I'm testing it on an iOS6 device, but can also test with iOS7 in the simulator.
Update 2:
Apparently the solution is to rescale the images to match the button rectangle.
The problem is that the button has a title that is positioned below the image to describe the functionality. In this case how could I put the text "floating" above the image ?
I'm assuming you're not programming in iOS 7?
Set the backgroundColor property of your UIButton to [UIColor clearColor]`;
If you've connected an IBOutlet to your UIButton then you can do something like this
[myButton setBackgroundColor:[UIColor clearColor]];
This will only work if you have a well defined transparent image. If you have some areas in your image that is not transparent then the above method will not work and you will simply need to edit your image again to make sure all the areas are actually transparent.
If you're in iOS 7, I believe the UIButton is already transparent and its up to your image to do the rest.
UPDATE 1
OP has requested for the title to remain above the image of the UIButton. Here is the solution for that,
What you need to do is set the backgroundImage property of the UIButton instead of simply setting the image of the button which will push the title down as you have found out.
Again I'm assuming you have an IBOutlet connected to your button; simply do the following:
Make sure you delete any image setting code in interface builder so that the properties are blank.
Then implement the code below
//This is the bit that does the magic - setting the background property
[myButton setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
//Then you can set the title as you require, the title will be centered floating above your background image.
[myButton setTitle:#"Localised String" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
And you will get something like this:
If you are using a proper PNG file with transparency in the file, this works automatically without changing the alpha value. If the PNG does not have the transparent area defined properly you will not be able to achieve this by a property (only by complicated drawing methods, which is probably not want you want).
You have to use a PNG image with transparent background. To remove background colour of a image, Let me suggest you a free tool "GIMP" I've been using it for a while.
As already been said - you can use semi-transparent PNG for image.
As for putting up title over the image - you would need to write some code.
You won't be able to do this in Interface Builder.
So you can easy create button
#interface MyButton : UIButton
#end
#implementation MyButton
- (void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:self.titleLabel];
}
#end
As for image, covering the whole area - it's better, when possible, to use background image rather image.
For partial background color transparency - you always can set semi-transparent background color for it in IB or in code:
[button setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];

Image on UIButton

I have an ImageView with a picture and a button centered within it. On the button I have a different UIImage and the background on the button is white. In Attributes Inspector I have selected the UIImage for the button and made the button custom but still I have white background on the button.
I want the background on the button to be the same as the picture on the ImageView. I thought it was enough to change the button to custom. I am using Xcode 5.0.2, is there anyone that could help me with this. This is what my ImageView + button looks like now:
And these are the settings for the button:
If you're asking how to make background for the button clear, from within the Attributes Inspector scroll down to the View section and change the background to Clear Color from the dropdown menu.
Also make sure your .png has a transparent background. If you've already set the background to Clear Color the issue could simply be that your .png has a white background rather than a clear one.
try
[button setBackgroundImage:[UIImage imageNamed:#"name" forState:UIControlStateNormal]
Add image that you use for ImageView to Background properties of your button.
The following should do:
yourButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage"]]

Custom the backgroud of a UIButton or UIBarButtonItem on segControl & bars

I'm trying to change the appearance of my UIButtons int the view as well as UIBarButtonItems in the NaviBar or toolBar or SegControl.
And here are 2 questions.
NO.1. How can I set customed background pictures to the buttons I mentioned while I can change their titles programmaticly? I mean I found that if I set the background of a button, the title seems to be concealed by the background image?
NO.2. I tried to add the text of the title directly on the png, ( which is actually a imperfect way since I need to change the title during the runtime). Anyway it works out both the image and the text, but the resolution seems to be reduced because the text became sort of blurred.
Can anyone give me some advices how to achieve it? Thanks a lot!
NO.1 The button title should not be obscured by the background image. Are you setting the button's image in code like this:
[btn setBackgroundImage:image forState:UIControlStateNormal];
? You may be instead setting the button's image property (which is different from its background image).
NO.2 You really don't want to be adding text to the button PNG, for the exact reason you mention. Buttons in iOS are designed to display images and text the way you want - put your energy into getting the built-in buttons working the way they should. There are umpteen billion tutorials out there about how to do this.

Resources