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
Related
I want to custom iOS back button throughout the application. So I add these lines in my file AppDelegate.m :
UIImage *backButton = [UIImage imageNamed:#"btn_return"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButton.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Here is the result :
As you can see, there is a white mark on the right.... Does somebody know why ? How can I remove it ?
The previous view controller has a white space as back button title (in his navigation item) because I don't want any label.
Maybe it's because of that ?! Is there an other solution to not see the default "Back" label ?
Thanks a lot for your help
[EDIT]
When I try the answer of Cy-4AH, I get :
Use in this way for
UIImage *image1 = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.menuBarBtn setImage:image1];
You need use
[[UINavigationBar appearance] setBackIndicatorImage: backButton];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: backButton];
if yours deployment target IOS 7+
Otherwise ask yours designer to add one pixel empty column in the right.
Use this code:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
UIButton *backButton;
backButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,35 ,35)];
backButton.imageEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 7);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(btnBackAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setImage:[UIImage imageNamed:#"your back button image name"] forState:UIControlStateNormal];
UIBarButtonItem *Rightbarbutton=[[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem=Rightbarbutton;
Use this code for hide default backbutton
self.navigationItem.hidesBackButton = YES;
Thanks to #DipenPanchasara, I changed my code by :
UIImage *backButtonImage = [[UIImage imageNamed:#"btn_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
GFloat leftInset = SYSTEM_VERSION_GREATER_THAN(#"9.0") ? -2.5 : backButtonImage.size.width -12;
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
(values in UIEdgeInsetsMake changed)
Here is the result :
The back button image is slightly flattened but it doesn't matter.
Thank you all for your suggestions ! :-)
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];
i'm having a problem with my UIBackbutton not appearing and i'm not sure what is causing it.
This is my code for making the back button image, it is in my first ViewController :
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
When you first enter the next view (done by a segue from a button) the button isn't visible but the text to go back is visible. Once i've hit the back button the image appears and it stays appeared on the next time i click it.
I'm not sure if this is an issue to do with my code, or the file it's in or if it's and iOS 7 problem.
You may have more luck using a UIBarButtonItem and setting the back button background explicitly.
The appearance proxy has been unreliable in my own attempts to get a back button working with iOS 7. I experienced the same issue with the background not showing up properly first time showing the button.
If you just need to change the colour, try this[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
If you want to change back button image you just need to write this 2 lines of code:
[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:#"icon-back"]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"icon-back"]];
And if you create sub class of UINavigationController it makes easy to customize you navigationbar and push & pop controllers
Try this:
self.navigationItem.hidesBackButton = NO;
if you're trying to add it to the Navigation bar then:
UIImage* image4 = [UIImage imageNamed:#".....png"];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[button setBackgroundImage:....png forState:UIControlStateNormal];
[button addTarget:self action:#selector(Selector)
forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithCustomView:button];
NSArray *arr =[[NSArray alloc]initWithObjects:action, nil];
self.navigationItem.rightBarButtonItems = arr;
I'm trying to customize my navigationbar's UIBarbuttonItem so that I'm using this im my Appdelegate.m
// didFinishLaunchingWithOptions:
{
UIImage *navBarImage = [UIImage imageNamed:#"nav-bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:5
forBarMetrics:UIBarMetricsDefault];
UIImage *barButtonSave = [[UIImage imageNamed:#"nav-bar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonSave
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault];
return YES;
}
When I'm done with this I just place a UIBarbuttonItem with Storyboard and I'm done.
The point is I noticed that the "Save" button changes between my views!
It looks like is moving down few pixels in my DetailView:
Why is That?
I still don't know why. but I solved giving my custom UIbarbuttonItem image the 30px default height.
[see similar issue UIBarButtonItem shifting downwards after a UINavigationController push
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"];