UINavigationBar back button without text and fixed size - ios

I have a custom png I set as the back button for the UINavigationBar like this:
UIImage *navBackgroundPortait = [[UIImage imageNamed:#"nav_bg_portrait.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundPortait forBarMetrics:UIBarMetricsDefault];
In my view controller in the viewDidLoad method I use this hack to display the back button with no title/text:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:nil action:nil];
But my back button png gets stretched in width, and I really would like it to not do that at all. I simply want to display a static png image as my back button on all view's with no text on it, and never ever have it stretched. Can I do this, and how do I do it?
Best regards
Søren

Use this:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
[backButton setImage:[UIImage imageNamed:#"nav_bg_portrait.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popNavigationControllerFunction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Because you tell image to get stretched with resizableImageWithCapInsets:
If you want to display image as it is, just do:
UIImage *navBackgroundPortait = [UIImage imageNamed:#"nav_bg_portrait.png"];

Related

How to globally customize back button?

Following up on this question, where the accepted answer describes a way to globally customize the UINavigationController's back button, using this code in the implementation of a category named UINavigationItem+MyBackButton.h:
#import "UINavigationItem+MyBackButton.h"
#implementation UINavigationItem (MyBackButton)
-(UIBarButtonItem*)backBarButtonItem
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: #"Back Button Text"
style: UIBarButtonItemStyleBordered
target: nil action: nil];
return [backButton autorelease];
}
#end
This code works fine for me as long as I only want to customize the title. When I want to get down and dirty and try to customize the background image, just nothing happens and I all I get the default back button showing the title of the previous UIViewController on the stack:
- (UIBarButtonItem *)backBarButtonItem
{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 18, 27); // CGRectMake(0, 0, 23, 36);
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
[backBarButton setTarget:nil];
return backBarButton;
}
I also tried to use UIAppearance with this code:
UIImage *imgBack = [[UIImage imageNamed:#"back"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But this just resulted in a distorted version of the back button that also still showed the title of the previous UIViewController .
So, my question is what is the easiest way to globally customize my back button such that it doesnt show the title of the previous UIViewController but rather only my custom image?
NOTE: I would like to avoid subclassing UINavigationController as well as having to set the back button explicitly within every UIViewController, actually the following code will work for this, if placed e.g. in viewDidLoad
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 18, 27);
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
So, what I am looking for is a truely global way to customize the back button exactly once.

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!

UIBarButtonItem appearance how to remove text?

I am customizing my nag bar button in iOS7 and I am using the following code:
// Customizing the NavBar Buttons
UIImage * btHome = [[UIImage imageNamed:#"Home.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 3)];
[[UIBarButtonItem appearance] setBackgroundImage:btHome forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It is working, but the view shows my home button with the button text over it, I do not want the text to show.
What else do I need to do to remove the text?
What I have is shown here:
Try to set
1) text color on a bar button item through appearance
2) use setImage: instead of setBackgroundImage: and do not use resizable image
Try with this code. May be it will help you.
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];
yourbutton.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[yourbutton addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourbutton];
self.navigationItem.leftBarButtonItem = backButton;
Sets the back button title offset for given bar metrics
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

Navigation Bar Item stretching image for button

I have setup a Navigation Bar in a storyboard with an UIBarButton for the right.
This is the code I use to update the image for this:
// Setup the right navigation bar item
[self.addGameButton setBackgroundImage:[UIImage imageNamed:#"addGameButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addGameButton setTitle:#""];
The image is here, it is 76x58 (at 2x). It is 38x29 (at normal).
When run on the device the image is stretching and I do not know why?
The backgroundImage property is meant to draw a stretchable image for the background of the button. If you want the image inside the button, you could try initializing the UIBarButtonItem with this:
UIImage *image = [UIImage imageNamed:#"images/global/add"];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setRightBarButtonItem:barButtonItem];
For you, it looks like you have an image that you want to replace the whole button with. You could also try this code, which will create a custom button as the view for your UIBarButtonItem:
UIImage *addImage = [UIImage imageNamed:#"images/global/add"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(0, 0, addImage.size.width, addImage.size.height)];
[addButton setBackgroundImage:addImage forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setRightBarButtonItem:barButtonItem];
In storyboard you can dragg a UIButton over the UIBarButtonItem and you can customise the UIButton as you like.
Try setting the frame of the button to the size you want.
Stuart, since you have setTitle:#"" the nav bar button will not display the text "add" but instead take up the space that was allocated to the text.. I am also yet to find a proper solution but a workaround for your problem is to set the font size to 0.1f. This will resolve your issue..
you need to set the navigation bar appearance in the "application:didFinishLaunchingWithOptions” method of AppDelegate.m, try adding the following code:
UIImage *buttonback = [[UIImage imageNamed:#"nav_back"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 22, 0, 0)];
[[UIBarButtonItem appearance]
setBackButtonBackgroundImage:buttonback forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor whiteColor],
UITextAttributeTextColor, [UIFont fontWithName:#"verdana" size:0.1f],
UITextAttributeFont, nil] forState:UIControlStateNormal];
Also Note that this will work with iOS 6, looking for a solution for compatibility in iOS 5

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