itsfaraaz
itsfaraaz

Reputation: 191

Why is the navigation bar content showing on iOS 11 but not IOS 10?

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.

Upvotes: 8

Views: 1360

Answers (2)

Changnam Hong
Changnam Hong

Reputation: 1679

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
    }

Upvotes: 5

Steven
Steven

Reputation: 329

Try the following steps:

  1. Change view controller hierarchy to TabbarViewController > NavigationController > ViewController1, ViewController2

enter image description here

  1. Add frame to label. Navigation bar in iOS 11 could configure label's frame by its intrinsicContentSize, but earlier iOS couldn't.

  2. Set titleView by self.navigationItem.titleView = titleView. Don't use self.parent?.navigationItem.titleView = titleView.

  3. self.navigationController?.navigationBar.isHidden = false is enough, and it's not necessary to call self.parent?.navigationController?.navigationBar.isHidden = false.

Upvotes: 0

Related Questions