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

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";

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];

Navigation controller with tab bar controller of navigation controller

I have a navigation controller, and its root view controller is a tab bar controller. In this tab bar controller I have two view controllers like this:
In the tab bar controller I have a custom navigation bar.
When I click the first item in tab bar, the navigation bar looks good
but when I click the second, I have a problem: below the navigation bar there is another navigation bar with red color.
Can some explain this for me?
If you don't want navigation bar of first navigation controller then from the interface builder (storyboard) select your root navigation controller (i.e. navigationcontroller that's embed with tabbarcontroller) and from attribute inspector uncheck shows navigation bar under Navigation controller! This will hide navigation bar for root navigation view controller!!
In your case you should hide and show navigation bar in viewWillDisAppear and viewWillAppear something like,
In viewWillAppear
self.navigationController.navigationBar.hidden = NO;
In viewWillDisAppear
self.navigationController.navigationBar.hidden = YES;
Do above things for your both viewcontroller of your tabbarcontroller!!
You can do navigationController.navigationBarHidden = true on the root navigation controller, or the child whatever suits you.
But the better will be if you use only one UINavigationController, and IMO navigationController of UITabBarController only.

Hide bar button items swift

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.

How to change UINavigationBar title without changing UITabBar title?

I have a UINavigationController embed on a UITabBarController. My goal would be to change the title of the UINavigationController's navigation bar while keeping the title on the UITabBarItem too. Tried changing the title of the UIViewController embed on the UINavigationController with:
self.title = #"NavBarTitle";
It does work, but that changes the UITabBarItem's title too.
Is there a way to separate those two titles?
Thanks!
try this
self.navigationController.visibleViewController.title = #"NavBarTitle";
or use
self.navigationItem.title = #"NavBarTitle";
or try this
self.tabBarController.navigationItem.title = #"NavBarTitle";

Change Split View Controller Navigation Bar Colour

I have a split view controller and I want to change the navigation bar colour.
Using the below in app delegate I have changed the colour of the detail screen (right).
navigationController.navigationBar.barTintColor = UIColor.orangeColor()
But I can't seem to figure out how to change the bar of the navigation controller (left).
Can anyone help?
Thanks
The template provides you a MasterViewController (the left) and a DetailViewController (the right). It is the same code in both the MasterViewController and the DetailViewController, likely in viewDidLoad. You have it right, although there is an optional you are missing:
if let navContr = self.navigationController {
navContr.navigationBar.barTintColor = UIColor.orangeColor()
}

Resources