Jonathan F.
Jonathan F.

Reputation: 2364

Create a custom left back button on UINavigationBar WITH the standard arrow on the left

When I create a custom back button, I use the following code:

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Yeah" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonPressed:)];
self.navigationItem.leftBarButtonItem = leftButton;

This works fine, and I obtain this result:

Screenshot of the word 'Details' where the back button would normally be.

I would have the same result, but with an arrow on the left, like this (when it's a standard back button, not a custom one):

Screenshot of the standard back arrow followed by the word 'Details'.

How can I simply add this arrow ?

Upvotes: 28

Views: 43383

Answers (6)

Jonathan F.
Jonathan F.

Reputation: 2364

Finally, here's the snippet I use to define the back button's title with the standard left arrow in the current view, not in the parent view :

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setTitle:@"Current View"];

    // Get the previous view controller
    UIViewController *previousVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];

    // Create a UIBarButtonItem
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"FooBar" style:UIBarButtonItemStyleBordered target:self action:@selector(yourSelector)];

    // Associate the barButtonItem to the previous view
    [previousVC.navigationItem setBackBarButtonItem:barButtonItem];
}

Here's the result :

enter image description here

Note : However, since it's not possible to add an action on a backBarButtonItem, you can refer to this great post if you want it to.

Updated for Swift

// Prev - no chevron...
//navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back !", style: .plain, target: self, action: #selector(backPressed))

// adds the chevron
let vc = navigationController?.viewControllers.first
let button = UIBarButtonItem(title: "Go Back", style: .plain, target: self, action: #selector(backPressed))
vc?.navigationItem.backBarButtonItem = button

Upvotes: 29

SomaMan
SomaMan

Reputation: 4164

For Swift, using Erzekiel's answer as the basis for this, you can simplify it to -

extension UIViewController {

    func setBackButtonTitle(to title: String) {
        let barButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
        self.navigationItem.backBarButtonItem = barButtonItem
    }
}

This should be called from the parent viewController, eg in viewWillDisappear -

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.setBackButtonTitle(to: "Back"))
}

Upvotes: 0

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

I use code like:

UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] init];
leftBar.title = @"Back";//Details
self.navigationController.navigationBar.topItem.backBarButtonItem = leftBar;

Upvotes: 0

Durican Radu
Durican Radu

Reputation: 1337

UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:FRAME_DEFINE
[backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[backButton setExclusiveTouch:YES];
[backButton setImage:[UIImage imageNamed:BACK_BUTTON_DEFAULT_ICON] forState:UIControlStateNormal];
[backButton setTitle:@"BACK" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *backMenuBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backMenuBarButton;

Upvotes: 3

Bladebunny
Bladebunny

Reputation: 1349

The easiest thing to do would be to set the title, in the parent controller (i.e. the one you want to nav back to). If you don't want this to be the same as the actual title displayed in that VC's view, you can change the title in viewWillDisappear to what you want on the next VC's back button, and then change it back to what you want in the parent in viewWillAppear.

If you are using storyboards, you can also set the back title directly in IB.

Finally, in order to create a custom back button, you can do something like:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Details" style:UIBarButtonItemStyleBordered target:nil action:nil];

...just be sure to do this in the presenting (or parent) view controller, not the view controller being loaded (the presented controller).

Upvotes: 17

Retro
Retro

Reputation: 4005

well you need to go with a background image and with title and

// Creates a back button instead of default behaviour (displaying title of previous screen)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(backAction)];

tipsDetailViewController.navigationItem.leftBarButtonItem = backButton;
[backButton release];

Upvotes: 0

Related Questions