IOS 5 How to change the color of back button in a navigation bar?

后端 未结 5 1364
感动是毒
感动是毒 2020-12-28 09:09

I want to change the color of back button of a navigation bar to make it look like this\"enter

相关标签:
5条回答
  • 2020-12-28 09:14

    The first line of jacob's answer didn't work for me because the backBarButtonItem was NULL. It is NULL because it's been created later automatically when switching to an other ViewController. At that time you can set the title of the button with

    self.title = @"nice title"; // self is here the view(controller) within the popoverController
    

    but you can't set the tintColor.

    What worked for me, was to create a new UIBarButtonItem without any style. Then set the title and color propertys and set it as backBarButtonItem.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
    backButton.title = @"go back - now!";
    backButton.tintColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0];
    self.navigationItem.backBarButtonItem = backButton;
    [okButton release];
    
    0 讨论(0)
  • 2020-12-28 09:20

    Best way I found to set it globally or locally is

        [[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];
    
    0 讨论(0)
  • 2020-12-28 09:26

    Set the backBarButtonItem's tintColor:

    self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor];
    

    TIP: If you want this to be applied to all UIBarButtonItem instances in your application by default, then you can use the new UIAppearance API to do just that:

    [[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
    
    0 讨论(0)
  • 2020-12-28 09:34
    [[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
    

    try this It is working for me...

    0 讨论(0)
  • 2020-12-28 09:35

    If you want to make the button look exactly like in your picture, you may use an image, too:

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                            forState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];
    

    The background image must be a resizable image for good results.

    0 讨论(0)
提交回复
热议问题