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.
Navigation bar contains the navigation buttons of a navigation controller, which is a stack of view controllers which can be pushed and popped. Title on the navigation bar is the title of the current view controller.
Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.
You need to set label's frame. titleView
is subclass of UIView. So It doesn't have intrinsic contentSize. However, iOS 11 provides intrinsic content size for titleView
. So you don't need to set its frame. Check this answer.
iOS 11 navigationItem.titleView Width Not Set
func title(text: String) -> UILabel {
let label = UILabel()
// add frame
label.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
label.text = text
label.textColor = UIColor.black
label.font = UIFont.boldSystemFont(ofSize: label.font.pointSize)
return label
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With