We are creating an application which requires different headers for different views all connected via an Navigation and Tab View Controller. The initial view has an image as the title. The second view has text as a title and the third also has text as the title.
We are using storyboards to build this application, here is the hierarchy of the controllers.
Navigation Controller --> Tab Bar Controller --> View Controller 1, View Controller 2, View Controller 3
Here is the code we use to display an image on the first view controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let titleView = UIImageView()
titleView.contentMode = .scaleAspectFit
titleView.image = UIImage(named: "logo_white_thin")
self.parent?.navigationItem.titleView = titleView
self.parent?.navigationController?.navigationBar.isHidden = false
self.navigationController?.navigationBar.isHidden = false
}
Here is the code we use to display text as the title for the other two view controllers.
override func viewWillAppear(_ animated: Bool) {
guard let uid = Auth.auth().currentUser?.uid else {return}
guard let username = users[uid]?.username else {return}
self.parent?.navigationItem.titleView = title(text: username)
self.parent?.navigationController?.navigationBar.isHidden = false
self.navigationController?.navigationBar.isHidden = false
print("Setting navigation bar title to ", username)
}
The title function is an extension built to return a label:
func title(text: String) -> UILabel {
let label = UILabel()
label.text = text
label.textColor = UIColor.white
label.font = UIFont.boldSystemFont(ofSize: label.font.pointSize)
return label
}
Now the issue is, when we test our application on iOS 11, the Navigation Controllers work properly and all appears well. When we test our application on iOS 10, the image and text from the navigation controllers magically disappear. Any idea why this is happening?
Here is an image of whats up: Picture of the issue. On the left, no title shows up (IOS 10) and on the right a title does show up (IOS 11)
I am running the latest version of Xcode with Swift 4. Thanks in advance for any help.