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.
The UITabBarController is loaded as a rootViewController from a UINavigationController. I have 6 Tabs and FifthViewController and SixthViewController comes under More tab and they push a DetailsViewController to show details. I am unable to show back button in DetailsViewController to go back to ParentViewController. I tried all the below options but, none of them is working.
I am loading UITabBarController as a initial view controller from Storyboard and have FifthViewController and SixthViewController as well from Storyboard. In FifthViewController I am setting Header
-(void)viewWillAppear:(BOOL)animated
{
self.title = #"Alerts" ;
self.tabBarController.title = #"Alerts" ;
}
In DetailsViewController I am setting Header
-(void)viewWillAppear:(BOOL)animated
{
//I tired all the below options to show back button
}
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.tabBarController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.tabBarController.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
Where am I doing wrong?
From your description you have it setup incorrectly.
The UITabBarController should be the app's rootViewController. Each tab should be a UINavigationController. Each navigation controller should be setup with the appropriate root view controller for each tab.
The tab bar controller itself should not be in a navigation controller.
You should not be setting the backBarButtonItem of any navigation item.
With the setup I describe, each tab has its own unique navigation. A user can be on tab two, for example, and move forward to the next view controller in that tab. And the tabs will remain visible. Now the user can go to any tab and later return to tab two and still be on the 2nd view controller for that tab.
You need a UINavigationController as the child of the UITabBarController, if you want it to function within the tab with a navigation stack and back button.
Normally, the back button of UINavigationbar will show a back arrow, follow by the previous page title, so how to only show the back arrow?
Assume that you have two viewcontrollers vc1->vc2
In vc1,in viewDidLoad
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
Screenshot
I am developing an IOS application. I am using navigation controller. If I push to next page then back button title not shown previous page title in IOS 7. Back button title always "Back" in IOS 7. I set all pages titles in viewWillAppear, viewDidload like below. But it is not working.
self.navigationItem.title=#"Previous Page Title";
self.title = #"Previous Page Title";
What can I set back button title with previous page title in IOS 7
Thanx
If the title is large, back button shows back only.Try with short titles,like
self.title = #"Test";
if you want Long title , go for custom back button.
In iOS 7 the back button length is constrained (I think to 11 chars). You can set a custom "back button title" like this, for example in viewDidAppear in the view controller with the long title (not the current view controller!):
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
If you want to force to always have the back button, but still let the system create it for you (will handle "Back" localization, abbreviate the button to a single "<" if the current title is really long, etc), then just use an absurdly long back button title:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" really long never shown title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Try this code:
[[UIBarButtonItem alloc] initWithTitle:#"Your Title Here"
style:UIBarButtonItemStylePlain
target:nil action:nil];
Or you can set an image for button like this:
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"arrow.png"] style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
self.navigationItem.leftBarButtonItem = barBtn;
And #selector method:
-(void)back:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
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"