Aysen  Eremeev
Aysen Eremeev

Reputation: 11

Multi line Large Title shows only first line in navigation bar with back button

Please tell me how to add a multi-line title in the navigation bar with a back button for iOS 16.4.

I have already read all the discussions on Stack Overflow regarding similar topics. I have tried solutions from @krunal, @Kaiv Mata, and others, but they do not work when there is a back button in the navigation bar. The solution from @Ankur Lahiry: item.setValue(true, forKey: "__largeTitleTwoLineMode") works, but it is not suitable because it seems to break pull-to-refresh and because it is a private API key, and Apple will reject my app upon release.

Here is my code:

guard let navigationBar = self.navigationController?.navigationBar else { return }
            navigationController?.navigationBar.subviews.forEach({ subview in
                subview.subviews.forEach { subsubview in
                    guard let label: UILabel = subsubview as? UILabel else { return }
                    label.text = "Multiline title with back button that not fit in one line"
                    label.backgroundColor = .yellow
                    label.font = UIFont.systemFont(ofSize: 16)
                    label.numberOfLines = 0
                    label.lineBreakMode = .byWordWrapping
                    label.sizeToFit()
                    navigationBar.frame.size.height = 50 + label.frame.height
                    navigationItem.title = label.text
                }
            })

I tried adding it in ViewDidAppear() and ViewWillAppear(). I also tried setting extendedLayoutIncludesOpaqueBars = false and tableView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0), but all of this was unsuccessful.

The screenshot shows how the second line is hidden:

Expected result:

Thank you in advance!

Upvotes: 1

Views: 103

Answers (0)

Related Questions