Change colour of arrow in Back Button iOS - 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]];

Related

iOS set default back button appearance

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];

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]];

Back Button does not color accordingly (objective c)

I’m currently working on an iphone/ipad app that has multiple navigation bar colours. In one part of the app the navigation bar has to be orange and in the other part it has to be purple. On the start screen of the app the navigation bar is hidden.
The colour of the navigation bar, back button and bar button is set in prepareForSegue according to the segue identifier for both sections of the app.
UIImage *navBackgroundImage = [UIImage imageNamed:#"Navigation-Bar-Orange.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
//Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:#"Back-Orange"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//Change the appearance of standard navigation button
UIImage *barButtonImage = [[UIImage imageNamed:#"Standard-Orange"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Futura-CondensedMedium" size:21.0], UITextAttributeFont, nil]];
When switching to the other part of the application (going through the segue again), the navigation bar and standard bar button will be coloured correctly, but the back button will have the colour of the previous part of the app. I’ve tried setting the colour on several occasions in the application (e.g. viewWillAppear and viewDidLoad), but the problem still occurs.
How can I fix this problem and show the correct button colour for both sections of the application? Is there maybe another (better) way to set different navigation bar colours?
Visual example of the occuring problem:
http://imgur.com/KgxKYOi,kJjC7Iv,wq2mAzf#0
You are correct. In fact, any text you set for the back button in code would behave similarly. The back button that is displayed actually gets its settings from the previous view controller. Simply shift your settings back one.
I've set the actual backbarbutton by making a new UIBarButtonItem and adding it to the UINavigationBar.
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:nil];
[barBtnItem setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = barBtnItem;
Thank you for guiding me in the right direction Marcus.

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

Customized back button appears on UIImagePickerController

I use the following code to customize the back button on the navigation bar throughout my application:
UIImage *backButton = [[UIImage imageNamed:#"backButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *backButtonOn = [[UIImage imageNamed:#"backButton_on"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonOn
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
It's working great except when I present a UIImagePickerController and enter an album in the photo library the back button is also the customized back button. How can I get back the original back button in the image picker?
[[UIBarButtonItem appearanceWhenContainedIn:[UIImagePickerController class], nil] setBackButtonBackgroundImage:[UIImage imageNamed:#"blank-button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
this is the correct way
Try using this:
[[UINavigationBar appearanceWhenContainedIn:[YourClassThatsNotAUIImagePicker class], nil] setBackButtonBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];
That should limit your appearance setting to only the classes you list and therefore leave the UIImagePickerController alone.
1.for swift and ios9(the above answer will be like) --
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UIImagePickerController.self]).setBackButtonBackgroundImage(UIImage(named: "blank-button"), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
2.if you want to modify backIndicatorImage of UINavigationBar
UINavigationBar.appearanceWhenContainedInInstancesOfClasses([UIImagePickerController.self]).backIndicatorImage = UIImage(named: "backButton")//or nil
3.if you want to modify backIndicatorTransitionMaskImage
UINavigationBar.appearanceWhenContainedInInstancesOfClasses([UIImagePickerController.self]).backIndicatorTransitionMaskImage =UIImage(named: "backButton")// nil

Resources