Question

I'm trying to set the BACK button for a pushed VC set within a UINavigationController stack. I use the following code, and it's not working - I still get the previous VC name appearing as the back button title.

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.title = @"VC Title";

    UIBarButtonItem* myBackButton = [[UIBarButtonItem alloc]
                                     initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];

    self.navigationItem.backBarButtonItem = myBackButton;

}

Anyone?

Was it helpful?

Solution 2

Try setting the title in the parent view controller's viewDidLoad

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];

self.navigationItem.leftBarButtonItem = customBarItem;

OTHER TIPS

in parent view controller:

- (void)viewWillDisappear:(BOOL)animated
{
    self.title = @"Back";
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"Title of your navigation bar";
}

Will do the trick

From Apple's documentation:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

Hope this helps.

self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Put Any Title"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];

Set a backBarButtonItem to the navigationItem of the previous viewController. Check this answer https://stackoverflow.com/a/25680043/111277. Check my blog post iOS Set Navigation Bar Back Button Title for detail analysis.

One more solution, which is very quick.

Override this method in your Base view controller and you will have back button on every pushed view controller. (Just do not add [super setTitle:title])

- (void)setTitle:(NSString *)title
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    [lbl setText:title];
    [lbl setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:lbl];
}

for storyboard solution;

click on the previous view controller's navigation item. then click on the attributes inspector on the right pane, then write " " or anything else on the backbutton area. this will tell view controller what to show when in the next(child) view controller. hope it helps

for code solution;

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"back off" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];
[[self navigationItem] setBackBarButtonItem:customBarItem];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top