1

I added the following to customize my back button in my navigation bar but the image just overlaps/duplicated:

https://i.stack.imgur.com/eyzJC.png

- (void)viewWillAppear:(BOOL)animated
{
    //take out the label following the button
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

    //set image for back button
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage  imageNamed:@"backButtonImage.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backButtonImage.png"]];

    self.navigationItem.backBarButtonItem = backButton;
}

It's supposed to be an image of one black arrow facing to the left.

Before it worked fine but now it does this when I simulate it. Any insight and help would be appreciated.

Thank you

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Ko Ki
  • 43
  • 1
  • 5
  • Why are you doing that inside `viewWillAppear:` instead of `viewDidLoad:`? That replaces the back button everytime the view will appear, but it should be done once. I'm thinking you could simply hide the back bar button item (`self.navigationItem.backBarButtonItem = nil;` or `self.navigationItem.hidesBackButton = YES;`) and then add a customized one with an `IBAction` that performs `[self.navigationController popViewControllerAnimated:YES];` – Alejandro Iván Oct 04 '15 at 01:27
  • A similar question was asked here: http://stackoverflow.com/questions/3506297/custom-back-button-in-uinavigationcontroller – Alejandro Iván Oct 04 '15 at 01:30
  • @AlejandroIván Don't seem to work. Can you provide the full implementation? – Ko Ki Oct 04 '15 at 04:14

2 Answers2

0

This should work for you.

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 50, 44);
back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:@"backButtonImage.png"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];

add a function:

-(void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
William Hu
  • 15,423
  • 11
  • 100
  • 121
  • should I still include it in my viewWillAppear than viewDidLoad? Also should I keep the line of code that takes out the label following the button? – Ko Ki Oct 04 '15 at 04:13
  • Both places are OK. Don't know about what you said, but this is just a '''back''' button. Just try to use it if is your requirement. – William Hu Oct 04 '15 at 07:49
  • for example this: UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; Should I take it out? – Ko Ki Oct 05 '15 at 19:08
  • also for action:@selector(back) I am getting an error: Undeclared selector 'back' – Ko Ki Oct 05 '15 at 19:14
  • I actually replaced 'back' to 'leftBarButtonItem' and it cleared. But now when I click on the back button I am getting 'Thread 1: signal SIGABRT'. – Ko Ki Oct 05 '15 at 19:29
  • Make sure what you want to do first, UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; seems this make no scene. – William Hu Oct 06 '15 at 03:56
0

Add this code

[UINavigationBar appearance].barTintColor = [UIColor colorWithWhite:1.00 alpha:1];

It should solve your problem.

SukruK
  • 560
  • 2
  • 8
  • 22