Adding both image and title to a navigation backBarButtonItem - ios

I'm trying to add an icon (and keep the title) to the back button of my navigation controller. It seems if I set the image of the UIBarButtonItem it hides the title, so I thought I'd try a custom view. I've tried
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[customButton setTitle:#"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;
but Apple docs say that the backBarButtonItem ignores custom views, so this doesn't work.
I also tried this:
self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;
but the leftBarButtonItem shows up one screen too soon and the hidesBackButton doesn't seem to hide the backBarButtonItem.
Is there another way to get both an image and title onto a navigation backBarButtonItem?

Very first hide the back button provided by UINavigationController by susing the code self.navigationItem.hidesBackButton = YES;
And set own button with image and title with the following code
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[customButton setTitle:#"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;

Related

Changing vertical position of UIBarButtonItem on iOS 8.2

Here is my navigation bar
I had a problem about changing vertical position of Settings UIBarButtonItem on my navigation bar. I would like to move the button item "Settings" down
Here is my code
UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
self.navItem.rightBarButtonItem = settingsItem;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0,-10) forBarMetrics:UIBarMetricsDefault];
I had tried it again and again. It seemed that it's not work
Could anyone suggest me how to move the button item "Settings" down ?
You can easily add a Custom Button to your NavigationBarItem, Here is the way,
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //create custom Button
[button setTitle:#"Settings" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[button setTitleColor:[UIColor colorWithRed:179.0/255.0 green:40.0/255.0 blue:18.0/255.0 alpha:1] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 20); //Button frame
[button addTarget:self action:#selector(yourCustomSelectorHere) forControlEvents:UIControlEventTouchUpInside]; //Add action method to the button here
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
[backButtonView addSubview:button];
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithCustomView: backButtonView]; //set button as UIBarButtonItem
self.navigationItem.rightBarButtonItem = barBtnItem; //set barBtnItem to rightBarButtonItem
By changing backButtonView.bounds values you can change the origin of the Button
Cited from apple's documentation on UIAppearance protocol
iOS applies appearance changes when a view enters a window, it doesn’t
change the appearance of a view that’s already in a window.
So if your code of setting UIBarButtonItem's appearance is after the navigationItem assignment code. It won't work

iOS navigation toolbar button with image and title

I need a toolbar button with small text under button icon.
I'm trying on viewDidLoad to do:
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"menu-search-active.png"] forState:UIControlStateNormal];
[customButton setTitle:#"Button" forState:UIControlStateNormal];
[customButton sizeToFit];
UIBarButtonItem* sb = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self setToolbarItems:[NSArray arrayWithObject:sb]];
But my button have only icon, no title visible.

How to get this UIBarButtonItem in the picture?

I want to get a button with short lines on UINavigationBar.
Please, see that on this image:
What is it? How to get it?
You'll want to instantiate a UIBarButtonItem with a custom view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 24, 24);
[button setImage:[UIImage imageNamed:#"your_image_filename"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTappedMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
This creates a UIButton with a supplied image. That UIButton is then set as a custom view for a UIBarButtonItem. Then, just set the barButtonItem on your navigation item.

Create a back bar button programmatically

I'm trying to create a back bar button programmatically, similar to the iOS 7 default back button. But when I add my back bar button to the navigation item. The Chevron plus is missing. How can I achieve this?
UIImage* image_back = [UIImage imageNamed:#"leftarrow.png"];
CGRect backframe = CGRectMake(15, 5, 15,21);
UIButton *backbutton = [[UIButton alloc] initWithFrame:backframe];
[backbutton setBackgroundImage:image_back forState:UIControlStateNormal];
[backbutton addTarget:self action:#selector(Btn_back:)
forControlEvents:UIControlEventTouchUpInside];
[backbutton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *backbarbutton =[[UIBarButtonItem alloc] initWithCustomView:backbutton];
self.navigationItem.leftBarButtonItem=backbarbutton;
[backbutton release];
-(IBAction)Btn_back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

Make image for back button fit the entire button

I'm developing an application where I use UINavigationController. I would like to set the image for a back button. I do this using the code below.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backArrow.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
But the image appears only in the middle of the back button. How can I set the image to span the whole back button?
For back button in UINavigationBar, if you add image it will show an image in center of the back buttobn only. You cannot add image in that too. So instead of doing that, hide your backbutton and add this code in your viewDidload Method to add a image for it and looks like what you need.
self.navigationItem.hidesBackButton = YES;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"back_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"back_pressed"] forState:UIControlStateSelected];
[button addTarget: nil action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 50, 30);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: button];

Resources