Why is the contents of the navigation bar displayed on iOS 11 but not iOS 10?

We are creating an application that requires different headers for different views, all connected through a navigation and tab display controller. The original view has an image as a title. The second view has text as a title, and the third also has text as a title.

We use storyboards to create this application, here is the hierarchy of controllers.

Navigation Controller --> Tab Bar Controller --> View Controller 1, View Controller 2, View Controller 3 

Here is the code we use to display the 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 the 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 problem is that when we test our application on iOS 11, the navigation controllers work correctly, and everything looks good. 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 what: Image of a problem. There is no name on the left (iOS 10), and the name appears on the right (iOS 11)

I am running the latest version of Xcode with Swift 4. Thanks in advance for any help.

+5
source share
2 answers

You need to set the label frame. titleView is a subclass of UIView. Thus, it has no embedded content. However, iOS 11 provides an internal content size for the titleView . Therefore, you do not need to set your frame. Check out 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 } 
+2
source

Try the following:

  • Change view manager hierarchy to TabbarViewController> NavigationController> ViewController1, ViewController2

enter image description here

  1. Add frame for tag. The navigation bar in iOS 11 can adjust the label frame using intrinsicContentSize, but previously iOS failed.

  2. Set titleView to self.navigationItem.titleView = titleView . Do not use self.parent?.navigationItem.titleView = titleView .

  3. self.navigationController?.navigationBar.isHidden = false sufficient, and you do not need to call self.parent?.navigationController?.navigationBar.isHidden = false .

0
source

Source: https://habr.com/ru/post/1273519/


All Articles