iPhone Set Tint Color of Back Bar Button Item

前端 未结 10 779
清歌不尽
清歌不尽 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

提交回复
热议问题