UINavigationController's children views don't have UINavigationItem to set the title - swift

I have a UINavigationController implemented to have the kind of pop/push functionality. However, on the children views I cannot set the title of the Navigation Bar, but rather only on the first view:
On the "Buzzy" page as seen above, when I select the navigation bar, the utilities tab shows me that it is a UINavigationItem and I can edit its title. However, on one of the children views, such as the one with "Tappable Area", the navigation bar cannot be selected and the title cannot be added.
How do I add the title for children views as well?

Drag a Navigation Item object from the Objects palette (cmd-shift-L) onto the view controller and select it.
You should now be able to set the title, add UIBarButtonItems etc.

try with self.navigationItem.title = "Your title"

Normally this is done in view did load on the view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "some title"
}
However, this only works if you have your view controller embedded in a UINavigationController. I highly recommend doing this instead of creating a navigation bar yourself. If you insist on creating a navigation bar yourself, you can change the title by doing:
navigationBar.topItem.title = "some title"

Related

Search bar behavior inside Navigation Bar is inconsistent in iOS 13

I am trying to include search bar inside the navigation bar through navigation item of the view controller.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Sticky Section Headers"
self.edgesForExtendedLayout = .all
self.extendedLayoutIncludesOpaqueBars = true
self.navigationItem.searchController = UISearchController()
setUpNavBarAppearance()
}
This view controller has a collection view and when user taps on a cell in the collection view, i am pushing the a new instance of same view controller(demo purposes) on to the navigation stack..
The behavior for the search controller has been inconsistent between the two view controllers. For the first/root view controller, the search bar shows up by default, but for the second view controller that is pushed on to the navigation controller, the search is hidden by default until user scrolls down.. I would like keep the search appearance behavior consistent across both the screens..
I am aware of the hidesSearchBarWhenScrolling setting to false would keep the search bar visible all the time, but i want to hide the search bar while user is scrolling.
Is there anyway to get the consistent behavior?
Here is the gist file, if you like to try the code:
https://gist.github.com/prasadpamidi/829e636d4697fda025bb0795ee81e355
Appreciate any help.

How to get the navigation bar to show large title upon a segue back?

I have a tab bar app where one of the views is a UITableViewController containing static cells as content with 1 section and 1 row.
I want the Large Title to be set to "Always," so I made the selection on the storyboard and the title was large on the simulator. Now when the user taps "Start Chat," the app will segue to the Virtual Assistant View Controller, where the Large Title is set to "Never" on the storyboard. Now the problem is that when the user segues back to the previous view controller with the "Start Chat" table view cell, the title is not large anymore.
It is interesting that when I set the table view to be scrollable, the title becomes large again upon dragging down the table view. I made sure the navigation bar on the Navigation Controller storyboard is checked with the "Prefers Large Titles." I am using Xcode 11, and this was not a problem when using Xcode 10.
I tried creating a custom class for the view with the start chat button and this code did not work in making the title large from a segue back:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
What else could I do? Any help will be greatly appreciated!
I'd use willMove(toParent:) to change the title back before the segue is performed.
override func willMove(toParent parent: UIViewController?) {
navigationController?.navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
}
Set the properties when setting up the UINavigationController, before presenting it. If you already presented the navigation controller, try doing this to force-update the navigation bar:
navigationController?.navigationItem.prompt = ""
navigationController?.navigationItem.prompt = nil
I took this workaround from this question.
In your particular case, it would be better to subclass the navigation controller and set those properties in its viewDidLoad method, so its properties (largeTitleDisplayMode and prefersLargeTitles) are set in a self-contained code.

Swift change tabBar title after touching a tabbar item

I have app with a tabBar and Navigation controller.
How can I change the tabBar title (visible in top application's window) when I touch it in tabbar?
For example i have these items in tabbar:
Pizza
Beer
Orange
Apple
After I click pizza I want to have pizza in the title app in the top menu.
How can I do this?
Depending on your implementation, one of the below methods should work for you.
self.navigationItem.title = "title"
or
self.navigationBar.topItem?.title = "title"
If you are using a custom tabbar made with UIButtons and container
view, then add this to the button action or if you are using a native
UITabBarController, then set it's delegate to self and call this on
the didSelectViewController delegate method of the UITabBarController.
.
EDIT
After seeing your code, you need to use this property :
self.tabBarController?.navigationItem.title = "Profile"
and call this in every view controller's viewWillAppear, example for ProfileViewController
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "Profile"
}
Also, make sure that in storyboard, you set the view controller's class to the respective code class like :
and remove the text from the custom navigation bar you used:

How to set title of Navigation Bar in Swift? [duplicate]

This question already has answers here:
what would be a proper storyboard example of combining nav bars and tab bars in one app?
(2 answers)
Closed 7 years ago.
I'm trying to set title of Navigation Bar in Swift, I set Tab Bar and in Navigation Bar nothing is showing, no button, no title, nothing. I used some code but it's not working while I use Tab Bar, and when I deleted Tab Bar, code is working and everything is ok with Navigation Bar, title is showing and buttons are showing.
Code that I used for title is:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "My Title"
}
And in Main.storyboard I connected Navigation Controller with Tab Bar Controller, as in picture.
So, how to fix this ? The problem is that Navigation Bar is not working while using Tab Bar.
Since the Tab Bar is actually the Root View Controller of the Navigation Bar, you need to set the UITabBarController's title instead in the viewWillAppear function so that it happens every time you switch tabs:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "My Title"
self.tabBarController?.navigationItem.leftBarButtonItem = settingsButton //This is the IBOutlet variable that you previously added
}
But a better way to do it would actually be to have it the other way around, like so
You should hook up a UINavigationController for each child of the UITabBarController, as it is more correct semantically, and simpler to maintain.
Your question is unclear, if the code you presented is from UIViewController that is kept inside UINavigationController and navigationBar is shown you can simply use:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Title"
}
In UINavigationController Class Reference Apple wrote this:
init(rootViewController: UIViewController)
Parameters
rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
But you can do it if you insisted.But in this case , your ViewControllers of tabbarController all shared the same title,which is the title of tabbarController.
A better way to do it is to give each of tabbarController's viewControllers a NavigationViewController.

Change tintColor of Tab Bar in UIView via Storyboard or Swift?

I've found a post on how to change the tint color of the tab bar button, but it assumes you are using a tab bar controller. I tried any way and did not make a difference. I am using a regular UIView and dragged a Tab Bar control on there. How do I change the button tint color in this scenario? The storyboard and code suggestions are not making a change. I tried to add these into my viewDidLoad ever, but neither had an effect:
self.view.tintColor = UIColor.orangeColor()
self.tabBarController?.tabBar.tintColor = UIColor.orangeColor()
I was able to change the nav bar buttons tints via the storyboard no problem, but the tab bar isn't having any effect. I am trying to match the changes I did to the nav bar:
If you are using a Tab Bar Controller, this will work for the tab bar background color:
tabBarController?.tabBar.barTintColor = UIColor.whiteColor()
And this for the color of the items within the tab bar:
tabBarController?.tabBar.tintColor = UIColor.blackColor()
If you are not using a Tab Bar Controller, and you just dragged a tab bar into your view controller: Control drag from the tab bar in your storyboard to your view controller's swift file to create a new referencing outlet. It should something like this if you're unfamiliar:
#IBOutlet weak var myTabBar: UITabBar!
Then use that same lines of code above just replacing a few things:
myTabBar.barTintColor = UIColor.whiteColor()
myTabBar.tintColor = UIColor.blackColor()