Navigationbar title and back button name are same? - ios

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";

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;

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

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"];

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 :

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)

Related

self.navigationItem.backBarButtonItem cannot be set

I have a TableViewController named TVC and a cell in it. When I clicked on cell, TableViewController will push a new ViewController (VC) into its navigation controller. In this case, I set the [self.navigationItem.backBarButtonItem setTitle:#" "] in the viewDidLoad function of the TVC. But in the new pushed ViewController VC, the back button still has the "back" text. Can anyone explain this?
BTW, the TableViewController TVC is also pushed by another view controller VC2. I set the same function [self.navigationItem.backBarButtonItem setTitle:#" "] in the viewDidLoad function of the VC2 and the back button text displayed in the TVC is correctly hidden.
What Bimala and Ted pointed are two ways of approaching the issue. The former is to set a custom BackButtonItem and the latter is to simply use the default BackButton. But setting the title of the screen to "" may not always be acceptable. So u can choose to do the same in ViewWillDisappear method.
OBJ C:
-(void) viewWillDisappear:(BOOL)animated{
self.navigationItem.title = #"";
[super viewWillDisappear:animated];
}
SWIFT:
override func viewWillDisappear(animated: Bool) {
self.title = ""
super.viewWillDisappear(true)
}
Also you can set the title back in ViewWillappear
OBJ C:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationItem.title = #"your title";
}
SWIFT:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
self.title = "your title"
}
That's because the back button takes the title of the previous view controller. When none set it defaults to back. So set the name of the TableViewController to an empty string as follows:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = ""
}
Its my example:
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[leftView setBackgroundColor:[UIColor clearColor]];
UIButton *leftViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftViewButton setFrame:CGRectMake(0, 0, 30, 30)];
[leftView addSubview:leftViewButton];
[leftViewButton setImage:[UIImage imageNamed:#"NavigationBarButtonBackDefault"] forState:UIControlStateNormal];
[leftViewButton setImage:[UIImage imageNamed:#"NavigationBarButtonBackPressed"] forState:UIControlStateHighlighted];
UIBarButtonItem *barButtonItemLeft = [[UIBarButtonItem alloc] initWithCustomView:leftView];
self.navigationItem.leftBarButtonItem = barButtonItemLeft;
from documentation:
If the title of your back button is too long to fit in the available space on the navigation bar, the navigation bar may substitute the string “Back” in place of the button’s original title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the leftBarButtonItems or leftBarButtonItem property of its navigation item—the navigation bar does not change the button title.
If you just want to hide Back button then try:
self.navigationItem.hidesBackButton = YES;
or
self.navigationController.navigationBar.topItem.hidesBackButton = YES;
If you want to set custom Back button then use:
UIBarButtonItem *barButtonItem = [ [UIBarButtonItem alloc] initWithTitle: #"Custom"
style: UIBarButtonItemStylePlain
target: nil action: nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = barButtonItem;

iOS action of navigation bar back button not working [duplicate]

Trying to customize my back button in a drilldown navigation controller.
On my one view controller I have an Add button where the code programatically generates a new UIViewController:
- (void)add:(id)sender
{
MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:#"MyAddViewController" bundle:nil];
[self.navigationController pushViewController:addController animated:YES];
[addController release];
}
This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?
Thanks
self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.
This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.
You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:
When this item is the back item of the
navigation bar—when it is the next
item below the top item—it may be
represented as a back button on the
navigation bar. Use this property to
specify the back button. The target
and action of the back bar button item
you set should be nil. The default
value is a bar button item displaying
the navigation item’s title.
So, if you were to change:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
To:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I believe you would get the desired effect.
You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:
- (void)dismissMyView {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Quit" style: UIBarButtonItemStyleBordered
target:self action:#selector(dismissMyView)];
}
If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.

iPhone - Add "Add" Button when Edit button is selected in UITableView

i have a table view in my application which shows some items. when i click on one item, a new table view appears (with navigation controller: push). So at the top of the Table view there is now the navigationcontroller with the automatic "back" arrow to get back. i have the "edit" button enabled on the right side.
Now i want when i tap on the edit button, the Back button should disappear and a "+" add button should be there instead of the back button. Is this possible?
Or it is possible to get the Edit and Add button on the screen at the same time?
thanks
This is easy enough. Override the setEditing:animated: method of your view controller. This is called when the Edit/Done button is toggled (assuming you are using the standard editButtonItem from UIViewController).
In this method you create an "add" button and make it the left bar button item. This will hide the back button. Remove the "add" button and the back button will reappear.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// Add the + button
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAction:)];
self.navigationItem.leftBarButtonItem = addBtn;
} else {
// remove the + button
self.navigationItem.leftBarButtonItem = nil;
}
}
Yep, this is the approach that I used:
Have a property for both the back button and the add button and set it in viewDidLoad:
self.backButton = self.navigationItem.leftBarButtonItem;
self.addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addPressed:)];
Now you just have to swap the buttons and update the TableView state accordingly when 'Edit' is pressed. Here i also change the 'Edit' button to 'Done':
- (IBAction)editBarButtonPressed:(UIBarButtonItem *)sender {
if (self.tableView.editing == NO) {
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editBarButtonPressed:)];
self.navigationItem.rightBarButtonItem = myButton;
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setLeftBarButtonItem:self.addButton animated:YES];
} else {
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editBarButtonPressed:)];
self.navigationItem.rightBarButtonItem = myButton;
[self.tableView setEditing:NO animated:YES];
[self.navigationItem setLeftBarButtonItem:self.backButton animated:NO];
}
}
Hope this answers your question. :)
br denrase
The back arrow button is your navigation controller button. So if you want to disappear the same button then you have to write this code below:-
self.navigationItem.hidesBackButton=YES;
Now if you want to add your custom button on the navigation controller then use below code:-
UIBarButtonItem *customButton =
[[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(yourMethod:)];
self.navigationItem.rightBarButtonItem = customButton;
You can programmatically hide the back button when your table view begins editing, and then add the "Add" button to the left of your navigation bar.
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed)];
[self.navigationItem setLeftBarButtonItem:addButton];
Then, when the user presses Done, replace the "Add" button with the Back button:
[self.navigationItem setHidesBackButton:NO];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:#selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#“back”, #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:#selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];

How to change text on a back button

By default the back button uses as a text on it a title of a viewcontroller.
Can I change text on the back button without changing a title of a view controller?
I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.leftBarButtonItem.title = #"Back";
Thanks.
Try
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.
Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:
(bottom) ControllerA -> ControllerB (top)
...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.
So it's more in the context of self.title, not like the other left/right-bar-button setters.
You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.
Thanks Marco... that helped me...
Here is what i did.
If you are using a tableView to navigate to different views... put the code:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
In your didSelectRowAtIndexPath method... of the first Controller... Controller A.
When you navigate to Controller B the button will have the title "Back".
The back button pulls its text from the title of the parent view controller.
In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.
For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."
in the viewDidLoad method of RootViewController.m:
self.title = #"Home";
in the viewDidLoad method of SecondViewController.m:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
If you want your back button to read, "Back," set the title of the parent view controller to #"Back";
This work better for me. Try :
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
If you are using storyboard:
Open StoryBoard
In Document Outline window find ViewController to which you want to
return to
Click on Navigation Item of that ViewController
In Attributes explorer change Back Button value to your custom tile
That is it, enjoy...
And in MonoTouch the following works (in ViewDidLoad of the parent controller):
NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];
This worked for me.
In your parent view controller, set the back button when view loads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Notice that we don't need to include autorelease at the end with the latest iOS version.
Hope this helps!
[self.navigationController.navigationBar.backItem setTitle:#"back"];
It works for me. You can replace "back" with something else.
This one worked for me if you don't want to have a title!
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!
self.navigationItem.title = #"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:
So I put the title change in calling view controller when I push the other view controller like:
self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:#"pushedVCIdentifier"];
self.pushedVC.delegate = self;
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = #"Back";
and in delegate callback function (which I invoke in viewWillDissapear):
-(void)pushedVCWillClose:(PushedVC *)sender
{
self.title = #"Previous Title";
}
If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".
//You can achieve this by setting the title in the previous view controller as shown below
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}
For to change back button title, refer below code
InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];
//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`
In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003
This worked for me:
self.navigationController.navigationBar.topItem.title = "Back"

Resources