iosuinavigationcontrolleruinavigationbaruinavigationitembackbarbuttonitem

How to make leftBarButtonItem looking like backBarButtonItem?


The default solvings are not appropriate:

  1. to change the previous ViewController title - I need to make my own function controlling the button touches up
  2. to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a default backBarButtonItem.

Solution

  • Here's an example to create a custom leftBarButtonItem:

    UIImage *buttonImage = [UIImage imageNamed:@"backbutton.png"];
    UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
    [aButton setImage:buttonImage forState:UIControlStateNormal];
    aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
    [aButton addTarget:self action:@selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    self.navigationItem.leftButtonItem = backButton;
    

    Hope it helps...

    Edit:

    Try this...

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:@selector(aSelector)];
    self.navigationItem.leftBarButtonItem = backButton;
    

    I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!