Question

Here's my code for removing the back UIBarButtonItem of a navigation bar and replacing it with a cancel button:

UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = cancelButtonItem;

Every example I've seen online doesn't hide the backButton before replacing it with a custom item. I may be wrong but it just seems like one unneeded line of code.

Was it helpful?

Solution

Did you try using UIBarButtonSystemItemCancel instead of UIBarButtonItemStylePlain in the style of your UIBarButtonItem?

Try this,

    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAction)];
    self.navigationItem.leftBarButtonItem = cancelButton;

Also if you have used self.navigationItem.backBarButtonItem in your parentViewController, it will show that button in the childViewController's leftBarButtonItem by default, if your custom leftBarButton in the childViewController is not added properly.

OTHER TIPS

Try this. Write this in your app delegates did finish launching.

LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];

    self.window.rootViewController = self.navigationController;
    self.navigationController.navigationBarHidden =YES;

// and use uibutton in your view controller at place of back button or cancel button
// and u can push or pop your view from there.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top