Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Interface Builder: UIButton title disappears when setting image

I have made a project previously where I set a UIButton to have both text and also an image (in interface builder).

I have created another project and I want to achieve the same thing, but when I set the button to have an image, the title disappears (it's not getting hidden behind the image).

I can't seem to figure out why this is so if anyone could shed some light on this subject I'd appreciate it!

Thanks.

like image 773
Grant J Avatar asked Dec 17 '12 16:12

Grant J


2 Answers

Change your button to custom type. put the image on then put the text in.

You will need to put your own background on it, but at that point you will be able to use the edge settings to position both the text and the image.

Every button that I have with image and text is set to custom and ultimately has its own bg image as well.

Oh, another thing I use. Is the cornerRadius. To give the view the rounded effect like the buttons have by default.

saveButton.layer.cornerRadius = roundingNumber.floatValue;

Additionally you should be able to use the borderWidth and borderColor in conjunction with cornerRadius to get a button that looks quite similar to the original button

saveButton.layer.borderRadius = 1.0f;
saveButton.layer.borderColor = [[UIColor blueColor] CGColor];
// These are not the exact values (or maybe they are) but you get the idea.

hope that helps.

Correction

After Spending a little time investigating, it appears that when you put an image in the button. the text is shoved off to the right.

Use the edge settings to bring it back over the image.

With the button selected, Look in "Attributes Inspector" > Button > Edge > (Dropdown "Title")

the equasion I have come up with is

[Edge inset for Title] Left = -(imageWidth * 2)

This should Left align your title with the image that you have put in.

Custom positioning will need to be done to make it look correct.

This can be done entirely in the Interface Builder and you can use RoundedRect button style

Here are some images demonstrating the change.

Here I set the Button type to "Custom" to remove the border and background.

Custom was set

Here I set the Top and Left in the Edge set for "Title" (Title is an option in the drop down)

Left and Top was set

EDIT

Newest Xcode has moved the size settings to a new location

enter image description here

like image 77
The Lazy Coder Avatar answered Nov 16 '22 18:11

The Lazy Coder


Just put the image in the background field instead. This way the text will appear in front of the image.

like image 28
jonypz Avatar answered Nov 16 '22 19:11

jonypz