Navigationbar title and back button name are same?

16,548

Solution 1

According to UINavigationItem Class Reference

"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."

So try this in your first VC from where you are pushing other VC

self.navigationItem.title=@"Recent Books";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back"
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
self.navigationItem.backBarButtonItem=backButton;

Solution 2

Let's say you are pushing your viewcontroller A -> B

In viewcontroller A try adding this code in viewDidLoad method

UIBarButtonItem *btn_Back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
 style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem=btn_Back;

So you will see your "Back" button in viewcontroller B

Solution 3

The following should solve your problem

-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.backItem.title = @"back";
}

You can also use this Objective-C syntax

[[self.navigationController.navigationBar backItem] setTitle:@"back"];

Solution 4

Just do it in this way , one of the easiest way I did find till now :

Create a custom back button and add it into the Navigation Bar left button item property :

- (void)viewDidLoad
{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"Backnavigation.png"]  ; // Here set the back button image
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 24, 24);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
}


//Handle the Back Button Event
- (void) handleBack:(id)sender
{
    // do your custom handler code here
    [self.navigationController popViewControllerAnimated:YES];
}

Also, doing this way, you don't need to change the title of NavigationBar every-time.

Here is the Back Button for your reference : enter image description here

Share:
16,548
Naveen
Author by

Naveen

Updated on June 04, 2022

Comments

  • Naveen
    Naveen almost 2 years

    I created one title for navigationbar in my storyboard project ,but when I moved to next viewcontroller the back button shows the same name as my previous navigationbar(Main navigationbar) title.? Can i set separate title and back button names?

    I tried following code but its not working ? why?

     self.navigationItem.title=@"Recent Books";
    self.navigationItem.backBarButtonItem.title=@"Back";
    
  • Douglas Nassif Roma Junior
    Douglas Nassif Roma Junior almost 9 years
    I have two BarButton customized on my first view, on the left to "Information" and the right to "Settings". And yet, I had to set a name for BackButton in the first view to. In my component tree now are three BarButton, but when I run the app everything worked normally. Simply makes no sense.