How do I add properties for the "back button" in the navigation bar? The button links from detail view and back to the master view. I can't find any code for the back button, and the button is hidden in storyboard view.
This back button is added automatically by the UINavigationController in iOS, you can customize some things like the name, e.g. if you want to change the title or remove it totally while keeping the icon, you can use the below code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Custom Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Leave the title #"" if you want no text; also you can customize the behavior of the button by adding your own method like this:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Custom Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonEvent)];
- (void)backButtonEvent {
// Your code here
}
Be careful, this should be added to viewDidLoad in the first controller, not the second one.
Related
Similar to the issue described here, with the exception that my TabBar is actually showing the BackButton on my NavigationBar is not. In the question I provided a link to the answer that solved the problem was that there was a NavigationController within a NavigationController, I do not have that so this is a different issue.
The basic flow of my storyboard is Login (UIView) - TabBar (UITabBarController) - NavigationController (UINavigationController) - Actual visible screen (UITableViewController) - New TableView where the issue occurs (UITableViewController).
On the new actual visible screen or the TableView the NavigationBar at the top shows just fine and I can click in the general area that the BackBarButton should be and it will go back, but no BackBarButton item is visible.
I tried changing the color, allocating it in the previous ViewController, making sure it was visible, etc. And none of them have shown the Back Button. It seems to be there but it is not shown.
Any ideas? From the other question that is similar to my issue this seems to be a iOS 9 specific thing.
EDIT from looking at the Debug View Hierarchy I can tell that the back button is in fact there but it just has no label or back arrow on it. How can I make the back label visible?
EDIT 2
Here is the viewDidLoad method of the view controller where the back button should appear.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem.backBarButtonItem setTintColor:[UIColor whiteColor]];
// Other unimportant stuff
}
The way I am segueing to this view controller is through the storyboard with a Push segue. Code is below.
- (void)segueToUser: (UIButton*)button {
long row = button.tag;
PFObject *PFQuote = [_recent quoteAtIndex:(row-1)/2];
PFUser *u = [PFQuote objectForKey:#"creator"];
_send = u;
[self performSegueWithIdentifier:#"showUser" sender:self];
}
Here is my prepareForSegue in the same file as the segueToUser
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showUser"]) {
BRETTFUserTableViewController *bfutvc = [segue destinationViewController];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
bfutvc.us = _send;
}
}
Here is a picture of a section of my storyboard.
The first view is a tabView
The second view is a navView
The third view is a tableView
The fourth view is a tableView
Since no one has seemed to have found a solution I will provide additional information. From my digging deeper into the Debug View Hierarchy I have found some more information regarding the backButton. I can see that the back arrow is intact there and there is a NSString next to it that seems to be nil, I do not know why it is nil but that is what I found. Here are additional pictures of what I have found.
In your
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
you have Write a code line
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
In Above line you have set title " " Empty String.
Instead of empty string use the title which you want to show on back-button item. fr ex:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
Check it might that help.
Adding a image of storyboard, how i have embedded the view controller in navigation controller.
or in case if you have put navigation appearance code anywhere in your app then also post that code.
In your viewDidLoad and prepareForSegue methods you have this :
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
Which replace the default back button of the navigation controller by a button without a label.
You should simply remove these 2 lines to have the default back button.
I have finally solved the issue. To accomplish this I had to go into the navigation bar for the first tableViewController and set the Back Button title to Back and then set the tint color (as it defaulted to clear) and now it works.
So, I have the below UINavigationBar :
[ (<- Back) Info (Cancel) ]
Now, what I need to do is change the Titles for all the Buttons / Title to One, Two and Three. I tried doing :
self.navigationController.navigationBar.topItem.title = #"One";
But this only updated the button on the left. How do I change all three?
self.navigationItem.title will change the center title, and setting a button with title to self.navigationItem.rightBarButtonItem will change the right button title.
And here's how I've changed the back button title. Note: this code must be used on the previous view controller (the one you'd be hitting the back button to return to), not the presently viewed controller:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}
Following the examples of the many duplicates for this questions, I can't seem to get it right.
I have a UINavigationViewController that has a LoginViewController as the rootViewController. Here I got a button with a segue (push) to a LoginInfoViewController.
In LoginInfoViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
//null
NSLog(#"%#", self.navigationItem.backBarButtonItem);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStyleDone
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
//not null, still the back button says: "Back"
NSLog(#"%#", self.navigationItem.backBarButtonItem);
}
You will need to set the backBarButtonItem of the controller you are going back to, not the controller you pushed. Move your code to the LoginViewController viewDidLoad method.
The navigation controller derives the back button for the navigation bar from the backBarButtonItem of the preceding controller in the stack. If the item is nil, it will use the value in the title property of same. If the title is too long to fit, the navigation bar may substitute the string "Back" in place of the title.
If your controller has a custom left bar button item, the navigation bar will ignore the backButtonItem property and title presenting the custom button instead.
Set the title of the back button on the view BEFORE. So, if you segue from LoginViewController, you set the back button title on the item before you segue to LoginInfoViewController
Example:
In the viewDidLoad method on LoginViewController:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Go back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
This means you're setting the button on the LoginViewController, not on the LoginInfoViewController.
I am trying to add a button to the navigation bar in the view, but it is not showing up.
I have:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"audio-controls"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlaybackView:)];
in a viewDidLoad.
EDIT: It is also important to note that I am also using storyboard.
EDIT: I think there is an issue with self.navigationItem... it's not nil but no changes are being made to it.
On iOS if you do not set the bar button item in the navigation stack manually, one is added for you with the previous controller's title and a back arrow. I want to keep the arrow only. e.g. if my previous controller's title was Hello, my current controller would have the leftbarbuttonitem as "< CustomTitle". Currently, I just have this:
UIBarButtonItem *goBackAndSaveButton = [[UIBarButtonItem alloc]initWithTitle:#"[insert arrow here]CustomTitle" style:UIBarButtonItemStylePlain target:self action:#selector(goBackAndSave)];
self.navigationItem.leftBarButtonItem = asdf;
Is there any way to control the title so I can set a custom title but still use the back arrow"
Rather than using a custom bar button item you could alternatively use one of the view controller life cycle methods like willMoveToParentViewController: as the trigger for your save. If you use willMoveToParentViewController: (when the parent is nil) you can save once when the view is removed. If you use viewWillDisappear: your save will run more frequently when pushing other views.
In the -viewDidLoad method of the controller you're pushing a new view controller from, use this code:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"CustomTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Then when you push your new view controller, its back button will have your custom title.