Adding images to buttons in interface builder - ios

I want to add an image to my button instead of text. Can I do this in interface builder? Any example I can look at?
Thanks
Deshawn

1:select the button
2:select Attributes Inspector
3:change type to Custom
4:There are two ways to do this using the interface builder
-1: Make a button image->include it in your project->set the image as the "image" of the button.
-2: Make a button background image->include it in your project->set the image as the "background" of the button->set the "Title" of the button to anything you want.

Add the image in your project.
In IB set the button type to custom.
Set the image in Image or Background box just below the Title box.
That's all.

I actually had to drag and drop the images folder into the Project Navigator and chose that the folder is a reference and only then, I could see the images in the Media Library.
From there the only thing you need to do is drag the picture where you want or change for the image name in the Attribute inspector.

Related

Setting button image also ends up adding the word "button" to image; how to fix

So I made some assets in sketch and have successfully imported them into Xcode. When I attempt to give a button the image of one of these assets, Xcode does just that, but it also ends up adding the word "button" next to my image (seen in picture). I have a feeling my settings in the inspector could be the culprit, however playing around with them has yielded no victory.
Would you be able to recommend any solution?
Thanks]1
Remove the Button title "Button" from the inspector.
If you want button title and background image then assign your image into Background field which is below Image field and if you don't want button title then assign that image in Image field and remove title Button.
See the picture. By default it will take the title as Button. delete it. This will resolve your issue.

How to create a custom icon for a UIBarButton?

Swift
I want to create a custom icon downloaded from the internet for a UIBarButton. Do I have to do this programmatically or can I some how find a setting via storyboard? If I choose "custom" it doesn't lead to further choosing of custom icons or anything so I'm not really sure what to do here.
I see there is a "Bar Item: Image" section. Is this what I will use?
http://prntscr.com/5x7gn0
Thanks!
Yes, the image (at the end of that link you posted) is where you would select your image. You need to put it into your project first though.
So steps to do:
Drag the image (hopefully it's in PNG format?) into the sidebar in the left and tick the checkbox titled "Copy items if needed" and click finish. Now you should see the image in your project.
Go to that place in interface builder that you have in the link (here: http://prntscr.com/5x7gn0) and select the image you added from the drop down list.
There is another way you can do this; programmatically, but it's slightly trickier. You still do step 1 but then instead of step 2, you can create an outlet to the Bar Button item and set its image like this:
barButtonItem.image = UIImage(named: "imageName.PNG")
If you need any clarification of what I've said, feel free to ask.
Loic

Handling Button states

Hello I have another problem/question. I have a custom control with a button on it:
I want to delete an image from it, so I removed the reference from Attributes inspector -> Image, so now my button in designer looks like this:
But when I launch it on simulator, when I press the button (just push it, not making a click ) the image shows up! Where does it come from? Are there any button states styles in project files, where this could be written?
Because button has two references - 1 outlet and 1 action and none of those are used to add image to the button! I just don't understand where from the image gets in...
You should remove the image for both normal and selected states of the button from the attribute inspector(check stateConfig attribute).

How do you set the selected image for a button in Xcode?

I know how to change the image programatically when a button is pressed, but there must be a way to do this non programatically in Xcode, but despite searching high and low I simply cannot see anywhere in Xcode where the selected image can be specified.
Try in your storyboard. The second selection (STATE CONFIG) down in attributes inspector should show "default" for your button. Change the sate (Highlighted, selected, disabled) and then change the image in the image box 8 down or background image underneath.
Open up the "Attributes Inspector" in your storyboard or XIB and the image name is an attribute you can set in your UIButton object.
Also make certain to make the "Title" empty or a string will be drawn over your image.

iOS how to change default onPress button color

I have been trying to figure out this problem for a while now and no avail. I have a program I inherited and it uses a a white background image for the buttons. You can change what image you want easily from UI editor. When the user presses down on that button the white background image is switched out for a different image, showing a blue highlight instead. For the life of me I can't find where that is being referenced at. I even tried adding a new button and it is the stock highlight color.
Where is that onPress reference probably at? Or where can I re-reference this new button to have a new onPress image background?
Thank you!
Select the button in Interface Builder. Open the attributes inspector for that button. Under Button you can choose the "State Config". Change that to "Highlighted". Now you can change the Image config for the highlighted state.

Resources