UIBarButtonItem with image and text, and a border? - ios

I would like to create a UIBarButtonItem with an icon and text. I've seen this solution and this one but when you set the background image it doesn't draw a border.
I don't really want to draw a border (that looks like the "Plain" style one) with my icon on it. Is there another way around this?

If you look at the first solution you linked to you will notice the following line
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* remix = [[[UIBarButtonItem alloc] initWithCustomView:btton] autorelease];
Hope that helps!

Related

UINavigationBar barButtonItem change back button image and title

Is it possible to change the back button on a UINavigationBar and change the title?
When I try to set the customView property, I get an image right next to the default button.
I use this code
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:nil];
I want the title to be "Back" which is easy enough to do in Storyboard. But the problem is that no matter if I use code above or use customView property, the default back button remains.
You can add a UIImage to the UIButton. And, then use it as a custom back button. Here's a quick example:
// Custom image
UIImage *backButtonImage = [UIImage imageNamed:#"button-background-image.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 100, 30)];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// Custom title
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(barPayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[backButton setShowsTouchWhenHighlighted:NO];
UIBarButtonItem *buttonOnBar =[[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonOnBar;
Note: You will loose the chevron (system-provided back arrow) which was introduced in iOS7. It goes as a title and chevron together presenting your previous view controller.
Update:
You can also use UIEdgeInsets to resize your image intelligently.
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 15, 10);
UIImage *backButtonImage = [[UIImage imageNamed:#"button-background-image.png"] resizableImageWithCapInsets:edgeInsets];
You can achieve what you want by setting the navigationItem.leftBarButtonItem of the view controller that's being pushed to the custom bar button item with the look you want. Good Luck!

Make custom Navigation Buttons glow when touched?

In my navigation bar I have two right button bar items. One is a custom button and the other uses the info light button type. Both work great but I would like to make the custom button have the default glow the same way the info button does when you touch it. Is this possible?
// Default info light btn
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBtnItem = [[UIBarButtonItem alloc]initWithCustomView:infoBtn];
// Custom Btn
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
[customButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
NSArray *rightBtns = #[customButtonItem, infoBtnItem];
[navItem setRightBarButtonItems:rightBtns animated:YES];
you just need to put one more line of code like:-
customButton.showsTouchWhenHighlighted = TRUE;
After this your code Output something like this:-

UIButton responds to event outside of frame [duplicate]

This question already has answers here:
UINavigationItem Back Button touch area too large
(2 answers)
Closed 9 years ago.
I have a navigation controller which I've added a UIButton to the navigation bar, but there's something wrong with it. Even if I tap way outside of the button, it still calls it's action.
This is how I create and add the button:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setFrame:CGRectMake(0, 0, 70, 30)];
//[backBtn setFrame:CGRectMake(20, 7, 70, 30)];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
Here's the tap range of the button:
http://www.flickr.com/photos/98950925#N07/9465901718
It is by default size of left button and right button . So we can do nothing with left and right button. Because its area of left and right button. If you wanna custom button in that case it will show only your given frame but it'll work in left button's whole area.
Try this.
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btn_back.png"] style:UIBarButtonItemStylePlain target:self action:#selector(Back:)];
[self.navigationController setHidesBackButton:YES];
[self.navigationItem setLeftBarButtonItem: customItem];

limit the touchable area of UIbutton

This is how I set button navigation bar
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)]; used frame same as image
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
self.navigationItem.leftBarButtonItem =addEdit;
Everything works perfectly but button get pressed when I touch out side of it. How to solve this is there any way so it get pressed only if I touch on it
This is that image
apple has set this thing in way so user can navigate smoothly. it is advisable not to make such design in which you are putting buttons near navigationBarButton. there are some way to do it but its not good to change this kind of things. its just like reply to message and delete message both button are near beside to each other
I believe that you got this issue because the image you are setting is smaller thant the actual size of the button. please either make the button size smaller or provide a bigger image.
I hope this helps you.
It seems it works like that only, still I came up with a solution.
One is you can hide your navigation bar and use a toolbar indeed.
Other is you can add another button after that and set it's enabled property to FALSE.
I don't know whether this is proper or not, but seems to fulfill your requirement at least.
Here is the code:
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIButton *addEditButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton1 setFrame:CGRectMake(63, 0, 30, 31)];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
UIBarButtonItem *addEdit1 = [[UIBarButtonItem alloc] initWithCustomView:addEditButton1];
addEdit1.enabled = FALSE;
NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
[buttonArray addObject:addEdit];
[buttonArray addObject:addEdit1];
self.navigationItem.leftBarButtonItems =buttonArray;

Create UIBarButtonItem with back button style

I'm looking for a way to create programmatically an UIBarButtonItem that looks like a back button of UINavigationBar.
Apparently seems like that the back button appears only after a push on the UINavigationController.
So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.
Ideas ?
the short answer is you cannot do it.
you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar.
I think you need to use image. and then set the image be the buttons backgroundImage. such as :
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
It gives you a psd file with image that you need.
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(custom text with back arrow)

Resources