letsbondiway
letsbondiway

Reputation: 541

Multiline navigation bar title truncating on iOS13

I have a view controller where I need to display multiline title on the navigation bar. For this, I have written a protocol like this -

import UIKit

protocol CustomNavigationBar {
    func setupNavigationMultilineTitle(titleText: String, prefersLargeTitles: Bool, largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode)
}

And then extended it -

extension CustomNavigationBar where Self : UIViewController {

    func setupNavigationMultilineTitle(titleText: String, prefersLargeTitles: Bool = true, largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode = .automatic ) {

        self.navigationController?.navigationBar.prefersLargeTitles = prefersLargeTitles
        self.navigationController?.navigationItem.largeTitleDisplayMode = largeTitleDisplayMode
        self.navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.black,
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .semibold)
        ]
        self.title = titleText

        if let navBarSubViews = self.navigationController?.navigationBar.subviews {
            for navItem in navBarSubViews {
                for itemSubView in navItem.subviews {
                    if let largeLabel = itemSubView as? UILabel {
                        largeLabel.text = self.title
                        largeLabel.numberOfLines = 0
                        largeLabel.lineBreakMode = .byWordWrapping
                        largeLabel.sizeToFit()
                    }
                }
            }
        }
    }
}

In my view controller, I conform to this protocol and inside viewDidAppear method, I call setupNavigationMultilineTitle method as follows -

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.setupNavigationMultilineTitle(titleText: "This is created for testing This is created for testing This is created for testing This is created for testing This is created for testing")
}

**

This works good on an iPhone running lesser than iOS13.

**

enter image description here

**

However, on an iPhone running greater than iOS 13, it just displays one line and then truncates.

**

enter image description here

Has there been any changes in the UINavigationBar in iOS13? I researched and found something about background color but nothing related to multi line title using prefersLargeTitles and largeTitleDisplayMode.

Can someone please help me getting this up on iOS13?

Thanks!!

Upvotes: 10

Views: 1124

Answers (0)

Related Questions