iPhone Set Tint Color of Back Bar Button Item

前端 未结 10 758
清歌不尽
清歌不尽 2020-12-08 22:15

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.backBa         


        
相关标签:
10条回答
  • 2020-12-08 22:36

    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

    0 讨论(0)
  • 2020-12-08 22:37

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

    1. Select your UINavigationBar (inside UINavigationController)

    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.

    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.

    0 讨论(0)
  • 2020-12-08 22:38

    Well you can certainly change the color of just the Back Button or perhaps any Bar Button on the Navigation bar. Here's a small snippet to do it.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
    backButton.tintColor = [UIColor blackColor];
    [self.navigationItem setBackBarButtonItem:backButton];
    [backButton release];
    

    Well this is just one of the ways for doing this. I hope this helps!! I know the answer for it is already accepted, but I thought to give a proper snippet to it. Cheers!!! Happy Coding!!

    0 讨论(0)
  • 2020-12-08 22:38

    What worked for me was this :

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];
    
    0 讨论(0)
  • 2020-12-08 22:40

    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)

    0 讨论(0)
  • 2020-12-08 22:40

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

        [navigationBar setBarStyle: UIBarStyleBlack];
    
    0 讨论(0)
提交回复
热议问题