Hide bar button items swift - ios

How can I hide my left bar button item?
In my storyboard I dragged a Navigation Bar onto my View Controller, then a Bar Button Item. Under certain conditions I want to hide the Bar Button Item.
None of this works:
override func viewDidLoad() {
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.leftBarButtonItems = []
self.navigationItem.setLeftBarButtonItems([], animated: true)
}

I dragged a Navigation Bar onto my View Controller
Well, don't! There is a big difference between a navigation controller interface, where you set the navigationItem, and a loosey-goosey navigation bar just sitting there in the interface, which is what you have.
Embed your view controller in a UINavigationController and do things the right way. Then setting your navigationItem and its properties will work as expected.

You can't access to self.navigationItem.leftBarButtonItem because you manually drag navigationBar from storyboard. I would suggest to do the following instead:
add an IBOutlet of BarButtonItem (eg: barButton) that you created in storyboard
barButton.title = ""
barButton.isEnable = false
This will hide your BarButtonItem, and you can simply show it back later.

Related

Setting titleView of UINavigationItem makes titleView disappear

I've set the titleView of a UIViewController's navigationItem after init(). After pushing the VC to UINavigationController, titleView appears correctly at first time. But when I change (re-set) a titleView to an other view, it suddenly disappears.
But when I push another view controller and navigate back, it suddenly appears.
Do I have to perform any actions after re-setting the titleView?
If you are not using tab bar controller, then in viewDidLoad
setting the title as self.title is better.I have mentioned Tab bar Controller because if you have a view controller (in a NavigationController) in a UITabBarController, then if you set self.title it overrides the name of the tab as well as the top title.
I think you maybe code like this:
self.navigationItem.titleView = self.yourView;
If your yourView is a custom class , it maybe remove from superView when you switch your viewcontroller to next one;
So code like this maybe solve your problem:
[self.navigationItem.titleView addSubview:yourView];

In Swift, I have 2 navigation bars but want to hide one of them, how to do it?

I currently have a viewcontroller that has two navigation bars because Its has a navigation controller both before and after a tab bar controller. I tried to have it so that the viewcontroller before the tab bar controller will present modally but by dong so the 2nd nav bar didn't work properly. The 2nd nav bar is a custom side menu that only appears to work if the first navigation controller is preset. Here is a pic
Is there a way so that only the bottom nav controller is visible?
If I understand your question correctly, you want to hide the ui for the enclosing navigation controller - i.e. the nav bar. You can do that by setting it hidden in the viewDidLoad() function of that view controller:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}

Navigation controller's toolbar not being hidden after enabling hide on tap?

I have a navigation controller where I have enabled hide on tap.It hides at first when I tap on the screen but when I tap again,the nav bar hides but the toolbar does not hide at all and it is obstructing my view. I have already tried settoolbarhidden and toolbar.hidden properties but it does not work.How do I solve this?
EDIT : I need to hide it only on this screen,I need the toolbar for other screens so thats why I have enabled shows toolbar.
EDIT 2 : Let me frame my question better.
When I enter the view controller :
Both navbar and toolbar hides because I have set it to hidden which is good
When I tap the screen :
Both navbar and toolbar shows because I have set it this way in the previous view controller.(If possible,Can I only show/hide the navigationbar on tap not the toolbar?
And lastly when I tap it again to hide both bars :
The navigation bar hides but the toolbar does not go away? This is my problem.
As Per your question you want to show tool bar on a particular viewController. View Controller viewWillAppear Function Hide ToolBar and viewDidDisappear show your tool bar it will show on other view controllers.
" Please check the navigation controller checkbox its disable or not.After that set this on your view controller before your profile view controller "
override func viewWillAppear(animated: Bool) {
self.navigationController?.toolbarHidden = true;
}
override func viewDidDisappear(animated: Bool) {
self.navigationController?.toolbarHidden = false;
}
I think it will resolve your issue.
I had the same problem.
The hideBarsOnTap only work if you placed smth in it. So if it is empty it will stay.
You could just put a blank imageView or Label there for example.
Or if you want it completely blank, your only option is to put a tabGestureRecognizer on your View!

ENSideSwiftMenu: how to use same Navigation Bar for all View Controllers

I am using Evgeny Nazarov's Swift Side Menu for my iOS app and was wondering if anyone has used this same library and able to have the same Navigation Bar for every View Controller that is called from the menu.
I put a BarButtonItem that toggles the sliding menu on my Root View Controller. My problem is that only my Root View Controller shows this Toggle Button. But I would like to have that same Navigation Bar with the Toggle button on every View Controller that is accessible when clicking an option on the sliding menu.
Has anyone had an experience achieving this? Thanks!
UPDATE 1
This is what happens when I added a NavigationBar and BarButtonItem to the other ViewControllers that aren't the RootViewController. The new Navigation Bar ends up under the existing Navigation Bar from the Navigation Controller.
The screen on the left is the Root View Controller and the one on the right is the View Controller when I select the second item on the menu.
UPDATE 2: SOLVED
override func viewDidLoad() {
super.viewDidLoad()
let menuItem = UIBarButtonItem(image: UIImage(named: "icon_menu"), style: .Plain, target: self, action: "menuBarButtonItemClicked")
menuItem.tintColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = menuItem
}
func menuBarButtonItemClicked() {
toggleSideMenuView()
}
You need to put a UIBarButtonItem on the navigation bar in every view controller, not in the root view controller.
I too have used the same design for my app and I solved it this way:
View every sect "Show the Attribute Inspector -> Top bar" the voice "Translucent Navigation Bar", then drag the "Navigation Item" and then "Bar Button Item" in the position you want. Of course every button should have its code or references to the ViewController.
Let me know if you have solved.

How to set the navigation bar title from view controllers inside tabs?

I have a tab bar controller embedded in a navigation controller.
I want to set the title in the navigation from the view controllers inside the tabs.
If I use:
self.title = "ABC";
or
self.tabBarItem.title = "ABC";
the title of the tab at the bottom is set to "ABC", but not the title in the navigation bar at the top.
However, I only want to set the title in the navigation bar, not the tabs at the bottom.
How can I do that?
You can set it using self.navigationItem.title.
self.navigationItem.title=#"ABC";
You have to pass the navigationItem up the chain.
The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.
The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem
So to recap:
UINavigationController title = topViewController.navigationItem.title
UITabBarController tabTitle = selectedViewController.navigationItem.title
UIViewController title = navigationItem.title
You are probably using the wrong element, try with the following example :
self.tabBarController.title = #"YOUR_TITLE";

Resources