Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to use the new large title system in iOS 11 using Swift. When the title gets too long (see image example), it adds ... instead of line breaking or shrinking the text size. How can I add a line break?

Example image with long title

Here is some of the code I'm using to set up the title:

self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: MyGlobalVariable.themeMainColor]
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 22)]
navigationItem.title = "Search by Name"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

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

It worked for me.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...