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.
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.
Here I set the Top and Left in the Edge set for "Title" (Title is an option in the drop down)
EDIT
Newest Xcode has moved the size settings to a new location
Just put the image in the background field instead. This way the text will appear in front of the image.
It sounds like you are setting the image for the button but not the background image so reverse that.
I noticed that if the UIButton colour is set to default, adding an image makes it white and thus invisible. Changing the colour manually makes it visible again.
Related
I began to work with Unity3D since one month and I'm trying to do an application which contains a menu, composed by a panel and several buttons. At this moment, I'm trying to customize the menu, I change the fill color of the panel and buttons, but until now I could not change the border color of the buttons. There is any way to customize the border's color of the UI buttons? I would appreciate your help. :)
The easiest way to add an outline to your button is to use the Outline component (doc).
Simply add the Outline component where your button image or text is. Then configure it how you like it :)
The above results in the following button:
No, you can not change just border color. If you use default buttons, take a look at Source image. It is basically a grey square with black borders. So when you change color of image black color remains black.
To customize buttons as you want you need either create nested UI elements:
or create sprites with colors you want and replace source image
I find nested UI elements to be the easiest way to do something like this. Simple example:
Start out by creating a panel and change the scale until you get the desired size for your button. This will be your "borders".
Next create another panel as a child of the panel you just made and change the scale to be just slightly smaller than your first panel and change the color.
In the Inspector for your panel, add component and type in Button and add it as a component.
Finally, add a text as a child of your panel for a label and you're done.
I am trying to use the auto size classes from the storyboard on xcode 7 to position UI elements in a controller. The problem that I'm running into is that when I try to use the "Add missing constraints" function (located at the bottom right corner of the console), it positions my UI elements correctly except for the last elements (pictures describe better). The first image below shows the storyboard file where I just want 3 buttons (stacked above eachother) to be the same width and length to be on the top right corner of any screen.
However, when I add constraints and run the simulations, it seems like the top two buttons are positioned correctly with the correct length and width but the third button is out of place (image below).
So my question is, am I forgetting a step to make all buttons position themselves? Or should I try to convert everything to a percentage and place UI elements based on the percentage of the screen (if so, how would I go about doing that)?
I've also tried adding another blank button (removing the button label) underneath the 3rd button and adding constraints like that but it didn't work for me. Let me know if you have any suggestions, thanks!
In you case, Autolayout the constraints you need to give to UIButton is 4 constrains.
Leading
Trailing
Width
Height
If you miss any of them, then surely you will get an error. So, what's your error is?
To the third UIButton, you have not given the height, while to the above two buttons you have given.
So, just remove the bottom constraint of UIButton and give the equal height to above UIButton.
FYI, never use Add Missing Constraints without any confirmation from your side.
Update:
Check this video to remove trailing or leading margin:
http://sendvid.com/1h8deg18
You can actually see the solution in action if you use the Preview screen while setting up your auto layout constraints. I just created a similar view and buttons and stepped through the process. I coloured the buttons and named them to make things obvious.
I added the three buttons. At this point, none of the buttons show up in the preview.
I then setup the auto layout constraints for Button1. If you want the buttons anchored to the top right, then you don't need to worry about the leading constraint. You need width, height, top, and trailing.
Now Button1 will snap to it's position in the top right corner of the preview screen.
Now do the same thing for Button2. Set width, height, top (vertical space to Button1), and trailing.
Button2 will now snap into place in the preview.
Now the same thing again for Button3. You anchored the first button to the top right of the screen. Then Button2 to the bottom of Button1 and the right edge of the screen. Then again for Button3. You could also align the edges of the 2nd and 3rd buttons, if you prefer that to trailing space.
Now you'll see in the preview that your buttons are correctly positioned, regardless of device.
As long as you specify height and width for each button, you don't need to worry about the left edge or the bottom edge of the screen at all. They each know to "stick" to the top right and they know what size to be.
** Note: If you're not familiar with the "Preview" option...
With your storyboard open, hold Option and select storyboard again to get another copy of the storyboard on the right side. Highlight the view controller you are interested in on the left side. On the right side, select the Preview option as shown below.
Now you have your storyboard and the preview side by side, so you can see the exact impact of any auto layout changes you make. You can also add or remove devices to the preview.
I have an UIButton (type:custom) in an UIView set up in the interface builder, add an image for the default state to it with retina sizes. When I try on my iPhone, I see the retina version of the image, when I highlight it by tapping it it gets pixelated while being highlighted.
The funny part is when I change the image of the UIButton by code, there is no pixelated error.
Does anyone know a workaround without loading all my button images by code? thnx!
In interface builder you can specify both regular state and highlighted state images: simply choose the required "State Config" and configure the button.
If you specified an image for Default state, and left the image for "Highlighted" state empty, and checked the "Highlighted Adjusts Image" property on, then the button will use the Default state image and gray it out when it's pressed.
However, if your Highlighted state image is also set, the button will use that instead of generating its own highlighting.
Other things to check: make sure both versions of the image are included in the project.
E.g. say you have "button_image.png" and "button_image#2x.png". Make sure both of them are in the project. Also when you specify the image in the interface builder, use the name of the standard definition image, cocoa will pick the 2x version automatically - i.e. you would put "button_image.png" as your button's image, not "button_image#2x.png"
Hope this helps
I have purchased the Glyphish set of icons.. but they are all dark. I was wondering if there's a way to set the PNG gradient colour when I set the image as the background of a UIButton?
I was thinking of something like when the button is disabled, it changes colour. Is there any way to replicate this functionality??
The simplest method would be to create copies of the icons you're using, and tint/modify them in your favorite image editor. Name the various images something like:
icon.png
icon-disabled.png
Then, you can set one image for each button state. In Interface Builder, just select the button and in the Inspector window you'll see a popup menu that says "Default State Configuration". The image(s) you specify here will be used for all the various button state by default. In the Background field, enter icon.png. Then select "Disabled State Configuration" from the menu and enter icon-disabled.png as the Background image. Be sure to set the menu back to Default State Configuration to avoid getting confused in future edits.
If you want to set the same programmatically, just use something like:
[myButton setBackgroundImage:[UIImage imageNamed:#"icon.png" forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"icon-disabled.png" forState:UIControlStateDisabled];
It's possible to take an image and modify it programmatically with a gradient, but unless you're doing it all the time and modifying the gradient based on user input or something, you'll save yourself a lot of headaches and time by just doing it this way.
I Have some code for the gradient... if you want simple color for different states of the button.. i. e
selected, disabled and all.
You can create different configuration from the interface builder.
Code which I have will give you gradient color for button but then you check the status of the button to use different gradient colors..
Make your button backgrounds with this free app: http://itunes.apple.com/us/app/uibutton-builder/id408204223?mt=8
I'm working on an iPhone app, and one of the buttons is supposed to have a text label on it that changes. When I set the text label programatically as seen below, it's shortened using '...' instead of displaying the full label -- even though there is plenty of room for it on the button.
self.accuracyButton.titleLabel.text=#"User defined location";
I suspect this may be related by the way it 'resets' to the XIB defined 'accuracy' label whenever I try to click it, but I'm not sure how to fix it or what is going on.
please try
[button setTitle:#"foo" forState:UIControlStateNormal];
Yesterday I had the same issue, the button showed the three dots, even if there was enough room.
Using setTitle:forState: instead of setting the property displayed the text correctly
You can change the amount of padding around the label by using this:
#property(nonatomic) UIEdgeInsets titleEdgeInsets
Not sure why it cuts off, but might I suggest simply using a UILabel over the top of the button? That allows much more refined control of the label boundary and size.