How I hide the rightBarButtonItem of a navigation controller when I want to navigate from tab-bar instead of Navigation Controller.
To Hide the right button:
self.navigationItem.rightBarButtonItem = nil;
And to show it:
If you setup the right button in your view controller by assigning it to self.editButtonItem then simply assign it again in order to show it:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
Just write this line in viewDidLoad or viewWillAppear of the class in which you want to hide the right bar button
self.navigationItem.rightBarButtonItem = nil;
Related
I want to set the common Bar Button Item on the right of the Navigation Bar, it should be display on all screens managed by Navigation Controller and it calls the same action.
Just for example, in Master Detail Application template, there is "addButton" on the right of the Navigation Bar, I want to display it on DetailViewController as well (it won't be work because action is missing though).
I have created a subclass of UINavigationController, in which I can change something like Navigation Bar color, but I can't set Bar Button Items. I can set Bar Button Items in each screen's ViewController so that I have to duplicate action for each screen.
Also I've tried to create a subclass of UINavigationBar, but I don't know if I can add Bar Button Item on it.
How to set common Bar Button Item on Navigation Bar?
Thanks in advance.
Another easy way to do instead of creating an extension
implement UINavigationControllerDelegate in root view controller
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let backBarButton = UIBarButtonItem.init(title: "HINIBO", style: .Plain, target: self, action: Selector("menuButtonAction:"))
viewController.navigationItem.rightBarButtonItem = backBarButton
}
You can use category to approach it. Let's say UIViewController+NavigationBar category
1.Create a category
2.Add a method, -(void)setNavigationBarItem method (in this case) in .h file.
3.implement the method in .m file to deal with the set Bar Button Items stuff.
- (void)setNavigationBarItem
{
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(something)];
self.navigationItem.rightBarButtonItem = searchItem;
}
4.In which viewController you want the NavigationBarItem, import the category header and call [self setNavigationBarItem] method.
I want to add buttons to my navigation bar through code, but they are not appearing for me. I think it might have to do with my navigation bar being inside a UIView.
My code in viewDidLoad:
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add)];
self.navigationItem.rightBarButtonItem = addButton;
Interface builder:
Any help would be greatly appreciated!
You shouldn't add a navigation bar directly in your view hierarchy. Rather, add a navigation controller, have your view controller as the root controller of the navigation controller, and set the navigation controller as the initial controller of the storyboard (or as the destination controller of segues, etc.).
My app has tab bar controller that loads 3 view controllers in its viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self setViewControllers:#[firstViewController,
secondViewController,
thirdViewController,
]];
}
I want it to show up with a view controller (homeViewController) that is different from these three controllers. When the tab bar is first loaded none of these three tab bars will be selected. I want to change them by pressing tab bar items and return to home view by pressing navigation left bar button.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:btnImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(setHomeView)];
How can I show the homeViewController when the tab bar controller is first loaded without added it to tab bar items?
The tab bar controller by default sets the first ViewController at index0 to be the home controller.
If you don't want the first ViewController of the application to live inside of the tabBarController then you have to create your HomeVC and set it as the entry point for the application, but the tab bar won't be present in that View.
Theres no way to present a view controller inside of the TabBarController unless it lives somewhere inside of those three navigation stacks.
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.
I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.
How is this done?
In your UIViewController
self.navigationItem.title = #"The title";
Swift 3 Version:
If you use a navigation bar without navigation controller, then you can try this:
navigationBar.topItem?.title = "Your Title"
Hope for help.
In viewDidLoad
self.title = #"Title";
Write the below code in viewDidLoad or may be better would be under initWithNibName:
self.navigationItem.title = #"title";
Thanks.
From Apple Documentation:
The most common way to use a navigation bar is with a navigation
controller. You can also use a navigation bar as a standalone object
in your app.
So If you have a UINavigationController, all what you have to do to set the title of the navigation bar (as explained in all previous answers)
self.navigationItem.title = #"title";
But If you have a standalone navigation bar that is created programmatically within an object of UIViewController, You have to set the initial appearance of the navigation bar by creating the appropriate UINavigationItem objects and adding them to the navigation bar object stack i.e.
Create an instance of navigation bar (UINavigationBar)
Create an instance of navigation bar item (UINavigationItem) that manages the buttons and views to be displayed in a UINavigationBar object. Note that you set the title at this step.
(Optional Step) Add right/left buttons (instances of UIBarButtonItem) to the navigation bar item.
Sample of Objective-C Code for the mentioned steps:
UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
/* Create navigation item object & set the title of navigation bar. */
UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name];
/* Create left button item. */
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;
/* Create left button item. */
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;
/* Assign the navigation item to the navigation bar.*/
[navbar setItems:#[navItem]];
/* add navigation bar to the root view.*/
[self.view addSubview:navbar];
For Swift version of the standalone navigation bar, check out this answer.
You can also use 👇🏻
[self.navigationController.navigationBar.topItem setTitle:#"Titlet"];
In your UIViewController's viewDidLoad
self.navigationItem.title = "The title"; //In swift 4
or
you can just
self.title = "The title"
will do the trick
you can also put the above statement in the app delegate to replicate globally.
This code did not work for me
self.navigationItem.title = #"Title here";
But this worked.
self.title = "Title Name"
Use it in ViewDidLoad method
Format :
#property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
Example :
self.navigationItem.title = #"Title here";
very important note : make sure you make the changes that mentioned above in parent view Controller's viewDidLoad method, or parents navigation bar in storyboard
Sometimes you might get a case where you are updating the title and it is not causing any effect, this can be when you have a custom view on the topItem therefore you might want to update it like this: (self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"