Navigation Item Title not Showing - ios

I have following code in class homeVC: UICollectionViewController to display the title in the navigation bar and it works as expected.
override func viewDidLoad() {
super.viewDidLoad()
.
.
.
self.navigationItem.title = PFUser.current()?.username?.uppercased()
.
.
}
But when I clicked on the button to go to another view controller and then came back to homeVC view controller self.navigationItem.title is not displaying anything. I'mm wondering why that happened?
The following code is to go to another view controller
#objc func followersTap() {
category = "followers"
// make references to followersVC
let followers = self.storyboard?.instantiateViewController(withIdentifier: "followersVC") as! followersVC
// present
self.navigationController?.pushViewController(followers, animated: true)
}
This is the view controller to go to , and in this view controller i don't want to show the title from previous view controller so I'm using self.navigationController!.navigationBar.topItem!.title = "" to make it empty and when I go back to previous view controller the title is also empty, why?
class followersVC: UITableViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.topItem!.title = ""
self.tableView.reloadData()
}
I am trying to not show the first view controller title on second view controller top left corner where back button is and keep first and second view controller title shows.

If your plan is to not show the title from a previous view controller, the solution is like this:
Change:
self.navigationItem.title = PFUser.current()?.username?.uppercased()
to:
self.title = PFUser.current()?.username?.uppercased()
And change:
self.navigationController!.navigationBar.topItem!.title = ""
to:
self.title = ""

Try to move your self.navigationItem.title = PFUser.current()?.username?.uppercased() in viewDidAppear(), this should help

Related

Column style UISplitViewController not working properly when added to a Tab bar controller

I tried adding a split view controller with triple column style to a UITabBarController and it shows up in collapsed state. The same code works fine when I use the older style of the split view controller. How do I get the 3-column split view controller to show up correctly on a tab bar controller?
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// This works fine 🤷‍♂️
let splitViewController = SplitViewController()
splitViewController.viewControllers = [ UINavigationController(rootViewController: PrimaryViewController()) , UINavigationController(rootViewController: SecondaryViewController())]
// let splitViewController = SplitViewController(style: .tripleColumn)
// splitViewController.setViewController(UINavigationController(rootViewController: PrimaryViewController()), for: .primary)
// splitViewController.setViewController(UINavigationController(rootViewController: SecondViewController()), for: .secondary)
// splitViewController.setViewController(UINavigationController(rootViewController: SupplementaryViewController()), for: .supplementary)
// splitViewController.show(.primary)
// splitViewController.show(.secondary)
// splitViewController.show(.supplementary)
splitViewController.title = "First"
let secondViewController = SecondViewController()
secondViewController.title = "Second"
setViewControllers([
splitViewController,
secondViewController
], animated: false)
}
}
here is the link to the project: https://github.com/anirudhbandi96/SplitViewTest
This is how the new three column split view controller looks
This is how the old 2-column split view controller looks
I couldn't even get the 2 column one to work inside a UITabBarController. But, if I just make a new UIViewController, that adds as a childViewController a UISplitViewController, then everything works just fine.
class MyViewController: UIViewController {
let split = UISplitViewController(style: .doubleColumn)
override func viewDidLoad() {
super.viewDidLoad()
addChild(split)
view.addSubview(split.view)
// add in your constraints to fix to same size here
// -- here --
split.didMove(toParent: self)
}
}

Change Navigation Bar Title when different tab is selected

I am still fairly new to xcode.
I am trying to programatically change the navigation title when different tabs are selected in my UITabBarController.
I have a UItabBarController that creates the tab bar, then I have separate UIViewControllers which have different content for each of the tabs - this part works fine, however I cannot get the navigation title to change when different tabs are selected.
Here is the code for the main tab controller.
// SUPER VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
// NAVIGATION ITEM
navigationItem.title = "Job Information"
navigationController?.navigationBar.prefersLargeTitles = true
//setup our custom view controllers
let jobInfo = page_jobInfo()
let shots = page_shotList()
let attachments = page_attachments()
let notes = page_notes()
jobInfo.tabBarItem.title = "Information"
jobInfo.tabBarItem.image = UIImage(named: "jobInfo")
shots.tabBarItem.title = "Shots"
shots.tabBarItem.image = UIImage(named: "shots")
attachments.tabBarItem.title = "Attachments"
attachments.tabBarItem.image = UIImage(named: "attachments")
notes.tabBarItem.title = "Notes"
notes.tabBarItem.image = UIImage(named: "notes")
viewControllers = [jobInfo, shots, attachments, notes]
}
Here is the code for the second tab button - The other 2 tabs are the same as this so didn't want to spam this feed with huge amounts of code.
// SUPER VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
// NAVIGATION ITEM
navigationItem.title = "Shot List"
navigationController?.navigationBar.prefersLargeTitles = true
}
Since your view controllers are embedded in UITabBarController, you should change its (tab bar controller's) navigationItem. Moreover, you should do that in viewWillAppear method instead of viewDidLoad, like so:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "Bookmarks"
}

Cannot select specific Tab Index to show in Tab Bar Controller, always shows 0th index

I have a tab bar controller with four tabs. I want to show the first item at the beginning. With a button click from the first item (view), when it is clicked, I want to show the second tab. How can I do that?
I created a custom tabbarController class and tried to give tabbarindex like below. I checked at the beginning without a button click but it didn't work. It always loads the first tab bar item.
class HTabViewController: UITabBarController, UITabBarControllerDelegate {
var controllerArray : [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.selectedIndex = 2
self.tabBar.tintColor = UIColor.red
// defineViewController()
}
}
Note : can we show specific tab item with a button click?
Since self is the UITabBarController, you need to set the selectedIndex on self, not self.tabBarController.
override func viewDidLoad() {
super.viewDidLoad()
selectedIndex = 2
tabBar.tintColor = UIColor.red
}
job done, add self.selectedIndex = needed_index at viewDidLoad() method

Navigation Item title issue

I've an UITabBarController connected with an UINavigationController related with an UITableView and finally connected with an UIViewController.
I've connected programmatically the UITableView with the UIDetailViewController using that code:
...
navigationController?.pushViewController(detail!, animated: true)
In my UIDetailViewController if I try to change the navigationItem.title like that:
self.navigationController!.navigationItem.title = "Hello"
Nothing happens. The navigation item doesn't change title.
You can change title of navigationItem title using this :-
self.navigationItem.title = "YourTitle";
or other way is :-
In UIDetailViewController write this line of code :
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "ok"
}

Can't get any navigation item in iOS

I have a question regarding the navigation bar.
As far as I understand from iOS: A view controller opened by a segue inherits the navigation bar of the parent view controller. Is this correct so far?
Is there a view controller within a stack "owns" the navigation bar in a complex segue stack (e.g. TableViewController that opens a TabBarController that opens ...)?
I very often run into the problem that I don't know where to get the actual navigation item in order to set the title or a bar button item.
In this case, I have the following controllers:
TabBarController
EventPostsViewController -> To display a list of posts, is a tabbed view within the TabBarController
CreatePostViewController -> To write a new post
So within the EventPostsViewController I can do this (and it works):
class EventPostsViewController: UITableViewController {
...
override func viewWillAppear(animated: Bool) {
...
// This solution works, but only for EventPostsViewController
self.tabBarController?.navigationItem.title = "text"
But within the CreatePostViewController, which is opened by a segue via EventPostsViewController, neither of this solutions work.
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
...
// Neither of these solutions works
self.navigationItem.title = "Text"
self.tabBarController?.navigationItem.title = "Text"
self.navigationController?.navigationItem.title = "Text"
How do I get the actual navigation bar/navigationItem?
Stupid simple mistake I repeat every time :)
I forgot to link my custom CreatePostViewController with the view controller using the interface builder.
This code now works:
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setNavigationBarHidden(false, animated: false)
// Set title
self.navigationItem.title = "Write Post"
// Add Submit button
var submitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "submitPost:")
self.navigationItem.rightBarButtonItem = submitButton
}
...
}

Resources