Customising UIBarButton Item Image - ios

So I am trying to customize a bar button item for this slideoutmenu I am creating. I want it to be a particular image. However I am having trouble doing this. Whenever I try to change the image programatically by saying
menuButton.image = UIImage(named: "myImage.png")
It appears to be all stretched. Whenever I tell it to be set to an image using the attribute inspector, it will do the same thing, and I am unable to resize it in the Main Storyboard and interface builder. I imagine the fix to be simple, but I am unable to figure it out. Is there possibly a way to customize the size of the navigation controller to give it more room? Does anybody have any ideas?

I misunderstood the question originally. It sounds like what you are asking is how you resize the image, because it's all stretched out. It might take a little adjusting, but you can set the image insets on the UIBarButtonItem programmatically:
menuButton.imageInsets = UIEdgeInsetsMake(5, 5, 5, 5)
This is just telling the button to draw the image inset 5pts from the top, 5pts from the left, 5pts from the bottom, and 5pts from the right. You can tweak those values until it looks how you wish.

Related

Xcode Button image top text below in a button not in code

I create a button set image title like below picture.
I want
image
title
image on the top title below is it possible without any code?
I cant just use insets because to another size will wrong
Adjust both the title insets and the image insets.
It will need some calculations, and auto layout will probably screw it up ;)
Probably better to create a subclass of UIButton with a .xib
click the link to view my screenshot

Center UIButton with image next to it

I'm trying to create a UIButton where the facebook icon is a part of the centering, however i can't seem to figure out how to make it part of the centering process. So far i've just created a UIButton with the text. I could just create a image next to it, however then it will not be part of the centering.
You can easily set image and title both for your button for different control state like normal,selected or highlighted etc.
So, set your image and title to your button from storyboard or programmatically there is no need to take separate imageview for image.
You can set edge insets for title and image both from interface builder or programmatically. you can manage top or bottom edge insets for title and image of button to manage it's vertical appearance!
check screen shot below,
Update : (as asked in comment)
For example,
this is the setup for my back button with image!
Provide button title and image for normal state. Then just change the left inset for image as per image

Shifting backButtonImage horizontally

What I need,
What I get, on adding backButtonImage,
I want to shift the backButtonImage horizontally? No answers on here seem to work for me.
Any suggestions will be welcomed.
EDIT
I am just setting back image and back mask in storyboard, here it is (nav_back)
Have a look at UIBarButtonItem Size Inspector in storyboard.
You should be able to shift image to the right by X points by setting image left inset to X and right inset to -X. This worked with bar button item with custom image but not sure if it will with default back button

can't set image for Bar Button Item

I'm using Objective-C. In Xcode, I set a image(.png) to a navigation bar button item. But no matter how I change the resolution of the image, it always going to be a blue pic, big or small.
It looks like this(the right top corner):
Does the picture cause the problem? How to solve it? Thanks in advance!
It is because of Barbutton's size is fixed.
So , my advice is add UIView as Barbutton item and then add UIButton and set image According to your required size.
Check output in below image.
Your Hierarchy will be something like this ,

Simple Horizontal Button Bar for iOS

I have a requirement for a very simple Button Bar.
It should take up the width of the screen.
It should allow at least 3
buttons.
The buttons should be of equal width and together take up
the whole width of the bar.
Each button should be tappable, but not
have a selected state.
The bar will be overlaid on a MapView and positioned directly above a TabBar.
Tapping a button will launch a Modal ViewController.
I thought about using a UITabBar and not allowing its tabs to become selected, but the HIG is pretty clear that this is not correct usage and UIToolBar doesn't allow the button widths to be set.
This seems like a very simple requirement but I can't see an obvious solution. Is there something I'm missing? Can anyone suggest a solution?
What's wrong with just creating a simple view that draws an appropriate gradient, and then adding three buttons of the appropriate size?
If you're feeling ambitious, or if this is something that you're likely to use more than once, you could even have the view create the three buttons. Call it ThreeButtonBar or something. Give it a constant height and adjust the width to match that of its superview so that you can use it in portrait or landscape orientation.

Resources