Arrow on BarButtonItem - ios

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.

Related

Back button on navigation bar

How do I achieve this kind of button? Can I do using navigation bar ?
You have to use a UINavigationController. Each time you push a new view controller using:
self.navigationController?.pushViewController(controller, animated: true)
the new controller gets a back button with the previous controller in the stack name.
Yes you can by giving navigation title in you setting view controller.
self.navigationItem.title = #"Settings";
yes you can achieve this by navigationBar. it is very easy in implement by UINavigationBar.
Objective C
// Set this in every view controller so that the back button displays back instead of the root view controller name
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
Swift
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)

Xcode Master Detail Application back button properties

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.

Change the title of backbutton in NavigationBar

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.

UIBarButton added to UINavigationItem not showing up

I am trying to add a bar button to my iOS app and can't get it to show up. I can see the bar in the Navigation Item's view hierarchy by setting a breakpoint. If it helps, I chose 'Embed Navigation Controller'. Any idea what's going on?
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(choosePreferredTerm:)];
[self.navItem setRightBarButtonItem:item animated:YES];
Here is the connection in IB
This is what the embedded Navigation Controller looks like:
This is what it looks like on the sim:
Try this rather than using custom outlet "navItem" also insure your application has a navigation controller in place
self.navigationItem.rightBarButtonItem = barButton;

Calling method on back button

I have a navigation controller that has a hidden navigation bar on the first view, and then appears for all children views. Problem is, I can't seem to hide it again if you go back to the first view.
I created a back button (so I could rename it) in the first view's init:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Logout" style:nil target:self
action:#selector(hideNavigationBar:)];
But the method never gets called. Why?
In your first controller, in -(void)viewWillAppear, say:
[self.navigationController setNavigationBarHidden:YES animated:NO];

Resources