iOS set default back button appearance - ios

I want to set a default behaviour for the navigation bar's back button, so it will look the same throughout the app. I also want to remove the default text, which I have already figured out.
I have tried the following without success:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"icon_backarrow"]];
//set default back text to #""

You should use UIBarButtonItem
UIImage *backButtonImage = [[UIImage imageNamed:#"icon_backarrow"]
resizableImageWithCapInsets:UIEdgeInsetsMake(4, 5, 4, 5)];
// Back button nav bar items
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-2.0f, 1.0f)
forBarMetrics:UIBarMetricsDefault];

Related

ios7 setBackButtonBackgroundImage image is still stretched

Using the following code to set a global navigation back button image:
float imageSize = 21.5; // #2x is 43px width
UIImage *barBackBtnImg = [[UIImage imageNamed:#"arrow-back-white"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
Original image: http://cl.ly/XJgD and
in-app (being stretched): http://cl.ly/XKc0
Any ideas why the image is still being stretched?
If anyone is interested, this was my fix:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"arrow-back-white"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"arrow-back-white"]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

Change colour of arrow in Back Button iOS

I'm trying to change the colour of the arrow for the back button in the navigation bar. I've been able to change the text colour, but not the arrow. Here is my code to change the text colour:
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
If anyone could advise me on how to change the arrow coliur, that would be very helpful!
You can use images like this to change the background of back button:
UIImage *buttonBack30 = [[UIImage imageNamed:#"button_back_textured_30"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
UIImage *buttonBack24 = [[UIImage imageNamed:#"button_back_textured_24"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 5)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack24
forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];
Source: RAYWENDERLICH
If you don't want to use images, you can change the tintcolor of navigation bar, because it determines the color of text of bar button item:
[[UINavigationBar appearance] setTintColor:[UIColor yourColor]];

Can't change the background for UIBarButtonItem in a separate View Controller

in my appDelegate:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[UIBarButtonItem appearance] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
and in separate view controller:
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:nil];
[backButton setBackgroundImage:[UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setBackBarButtonItem:backButton];
So on the child view controllers the title of the back button is changed but the background is taken from the appearance in appDelegate. I need a custom background for one view controller. How can I solve this?
edit#1:
now appDelegate looks like this:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[SettingsListViewController class], nil] setBackButtonBackgroundImage: [UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
You can achieve this by adding constraint to the appearence as follows:
[UIBarButtonItem appearanceWhenContainedIn:[YourOtherVC class], nil] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Your code should be look like as follows:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[[UIBarButtonItem appearanceWhenContainedIn:[SettingsListViewController class], nil] setBackButtonBackgroundImage: [UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

IOS Custom UIBarbuttonItem change position in detail view

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

iOS navigationbar setBackBarButtonItem to custom button

I need some assistance with how to customize 'backBarButtonItem'
And no I dont want to use the LeftBarButtomItem since I want to inherit the style and keep the transition from the 'BackBarButtom'
Okey, so what I got is:
UIBarButtonItem* barbtnItem = [[UIBarButtonItem alloc]initWithCustomView: [ArrivalProto ArrivalBtnTypeBack]];
[ArrivalProto ArrivalBtnTypeBack] gives me back a custom UIbutton with both a setBackgroundImage an a setImage.
And then all I want to do is to add this to customize the BackBarButton:
[self.navigationItem setBackBarButtonItem: barbtnItem];
But nooo. Just the the plain normal backbutton :(((
While this seems to work:
//self.navigationItem setLeftBarButtonItem:barbtnItem];
And this as well:
UIImage *btnTrnspBgrImg30 = [[UIImage imageNamed:#"trspBlackBtn30"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:btnTrnspBgrImg30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Info:
[self.navigationItem setBackBarButtonItem: <#(UIBarButtonItem *)#>];
[self.navigationItem setLeftBarButtonItem:<#(UIBarButtonItem *)#>]
As of iOS5 we have an excellent new way of customizing the appearance of almost any control using the appearance proxy, i.e. [UIBarButtonItem appearance]. The appearance proxy allows you to create application wide changes to the look of controls. Below is an example of a custom back button created with the appearance proxy.
Use the example code below to create a back button with custom images for normal and highlighted states. Call the following method from you appDelegate's application:didFinishLaunchingWithOptions:
- (void) customizeAppearance {
UIImage *i1 = [[UIImage imageNamed:#"custom_backButton_30px"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];
UIImage *i2 = [[UIImage imageNamed:#"custom_backButton_24px"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2
forState:UIControlStateNormal
barMetrics:UIBarMetricsLandscapePhone];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsLandscapePhone];
}
This is just a quick example. Normally you would want to have separate images for normal and highlighted (pressed) state.
If you are interested in customizing the appearance of other controls, some good examples can be found here: http://ios.biomsoft.com/2011/10/13/user-interface-customization-in-ios-5/

Resources