iPhone Set Tint Color of Back Bar Button Item

2019-01-17 16:39发布

I am trying to set the tint color of the back button within a navigation controller, but nothing is working. I have tried

[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set

But it stays the default tint

10条回答
疯言疯语
2楼-- · 2019-01-17 16:54

It always work for me:

  1. self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];

  2. GET DEFAULT BACK BUTTON

-

(UIBarButtonItem*) logicToAddBackButton
        {
            UIImageView *imageView;

            // [imageView setTintColor:[UIColor redColor]];
            UILabel *label=[[UILabel alloc] init];
            if (WHITEBACK) {
                imageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBackIPAD"]];
                [label setTextColor:[UIColor whiteColor]];
            }else{ //DEFAULTBACK
                imageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBack"]];
                [label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];

            }

            [label setText:@"Back"];
            [label sizeToFit];

            int space=6;
            label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space,
    label.frame.origin.y, label.frame.size.width,
    label.frame.size.height);
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space,
    imageView.frame.size.height)];

            view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width,
    view.bounds.size.height);


            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y,
    view.bounds.size.width, view.bounds.size.height)];
            button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width,
    view.bounds.size.height);
            [button addTarget:self action:@selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
            [button addSubview:imageView];
            [button addSubview:label];


            [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                label.alpha = 0.0;
                CGRect orig=label.frame;
                label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width,
    label.frame.size.height+10);
                label.alpha = 1.0;
                label.frame=orig;
            } completion:nil];

            UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button];


            return backButton;
        }
  • BACK BUTTON ACTION

    (void)eventBack { [self.navigationController popViewControllerAnimated:YES]; }

  • UiNavigationBack Image (Please change colour of image as require with same size [25X41 144pixels/inch] to get default look) enter image description here

查看更多
走好不送
3楼-- · 2019-01-17 16:54

What worked for me was this :

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];
查看更多
太酷不给撩
4楼-- · 2019-01-17 16:55

Swift way:

It changes all items in the nav.

In AppDelegate do something like this:

let navControl = UINavigationBar.appearance()
    navControl.tintColor = UIColor.grayColor()
查看更多
你好瞎i
5楼-- · 2019-01-17 16:58

If you want to set it to predefined style you can use

    [navigationBar setBarStyle: UIBarStyleBlack];
查看更多
\"骚年 ilove
6楼-- · 2019-01-17 17:01

If you prefer to do it in Interface Builder without writing code:

  1. Select your UINavigationBar (inside UINavigationController)

enter image description here

  1. In Attributes Inspector, find "Tint" - this is what sets Back Button color. Note that "Bar Tint" is not the same - this sets Navigation Bar background color.

enter image description here

It is important to set Back Button appearance this way, and not try to implement leftBarButtonItem, because implementing leftBarButtonItem will disable the built-in back navigation gestures, like swipe to go back.

查看更多
一夜七次
7楼-- · 2019-01-17 17:05

I believe barButtonItem is read-only. (I'm not too sure about this, someone correct me if I'm wrong)

To give a tint colour to these buttons, this is what I would do:

In your App Delegate, add these lines of code:

UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour

The first line sets an appearance proxy, so I believe this works too, if you like less code:

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

I hope this works for you! (This changes all instances of UIBarButtonItem)

查看更多
登录 后发表回答