Reputation: 1251
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";
Upvotes: 5
Views: 16624
Reputation: 413
You can use the below code in the viewWillAppear(animated:Bool) method of the view controller in which you want to set the backButton title of you choice :
self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back Title", style: .plain, target: nil, action: nil)
Upvotes: 0
Reputation: 2399
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"];
Upvotes: 2
Reputation: 4517
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 :
Upvotes: 1
Reputation: 9836
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;
Upvotes: 19
Reputation:
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
Upvotes: 6