Unable to change the color of the text and text of the navigation button back

I am trying to set the text color in the stand (to red) of my navigator using the code below. (NOTE: The code below is already in the "previous" view controller, in this case in the "Popular" view controller:

//Customize Back button UIView *backButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 40)]; UILabel *backButtonLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 40)]; backButtonLabel.text = @"back"; backButtonLabel.textColor = [UIColor colorWithHexString:@"cf212a"]; [backButtonView addSubview:backButtonLabel]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:backButtonView]; self.navigationItem.backBarButtonItem = backButton; 

However, I do not get the effect that I want. I still get the default white text in the lining. The text also does not change to "back".

enter image description here

How can i solve this?

+4
source share
7 answers

This is because backbuttonitem is a uibarbuttonitem that will be displayed on the next view controller, which will be placed on top of your current view controller, not the current controller.

If you want to set up a stand for the current view, as you are trying to do in your previous code, than simply move the code to the view controller that appears in front of it (below it in the stack), so when you click the view controller for which you want to show custom back button that was installed in the previous view controller. This is because the "Back" button refers to the previous view controller, which is about to click on the new view controller ...

+4
source

I believe that only the title property has a value in the back button element; this means that you have a long title in the middle and a shorter title in the back.

The easiest way to add a custom back button is to set UINavigationItem.leftBarButtonItem ; You will need to set its goal / action accordingly.

+1
source

I just put together a simple subclass of UIViewController that adds a custom back button that allows you to change the colors of the text. It basically adds some willAppear / willDisappear logic to animate the back button as the UINavigationController does, using the leftBarButtonItem property.

https://github.com/typeoneerror/BBCustomBackButtonViewController

+1
source

It worked for me.

With this, you can change the color of all navigation buttons:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

Replace redColor as follows to adjust the color of the buttons:

 colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this 

Be sure to put this in the view controller that pushes. Not a view controller where you want to see this button color. As Robin explained.

+1
source

The best way to find it globally or locally

  [[UIBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 
+1
source

The problem is that you do not have a good way to set the button font and colors globally. This is in current iOS 5. For this, I use typeoneerror https://github.com/typeoneerror/BBCustomBackButtonViewController mentioned here.

However, there is a good way to set the text and color tones of the buttons on the navigation bar on a global level without affecting the appearance of the buttons and menus. I use code like:

 [[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleColor:[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] forState:UIControlStateNormal]; [[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.75] forState:UIControlStateNormal]; [[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] titleLabel] setShadowOffset:CGSizeMake(1.0, 1.0)]; 

In one of my projects. I call it from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

0
source
 self.navigationItem.backBarButtonItem.tintColor = [UIColor blackColor]; 
0
source

All Articles