如何在导航栏中设置多行大标题? (iOS 11的新功能)

问题描述 投票:10回答:4

我正在其中一个应用程序中的导航栏中添加大标题。问题是标题有点长,所以我需要在大标题中添加两行。如何在导航栏中添加两行大标题?

这不是关于默认导航栏标题!这是关于iOS 11中引入的大标题。因此,请务必考虑大型标题来添加建议。谢谢

enter image description here

ios swift uilabel uinavigationbar large-title
4个回答
14
投票

获取导航项子视图并从中查找UILabel。

试试看,看看:

self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic

self.title = "This is multiline title for navigation bar"
self.navigationController?.navigationBar.largeTitleTextAttributes = [                     
                                NSAttributedStringKey.foregroundColor: UIColor.black,
                                NSAttributedStringKey.font : UIFont.preferredFont(forTextStyle: .largeTitle)
                                ]

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
         }
     }
}

结果如下:

enter image description here


13
投票

根据@krunal回答,这对我有用:

extension UIViewController {

func setupNavigationMultilineTitle() {
    guard let navigationBar = self.navigationController?.navigationBar else { return }
    for sview in navigationBar.subviews {
        for ssview in sview.subviews {
            guard let label = ssview as? UILabel else { break }
            if label.text == self.title {
                label.numberOfLines = 0
                label.lineBreakMode = .byWordWrapping
                label.sizeToFit()
                UIView.animate(withDuration: 0.3, animations: {
                    navigationBar.frame.size.height = 57 + label.frame.height
                })
            }
        }
    }
}

在UIViewController中:

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "This is a multiline title"
    setupNavigationMultilineTitle()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    setupNavigationMultilineTitle()
}

并在大标题上设置字体和颜色:

navigation.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: .red, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 30)]

-1
投票

在这里你可以在UILabel中添加一个多线NavigationTitle,你可以通过代码中的某种自定义来实现它并将UILabel放在self.navigationItem.titleView

    let label = UILabel()
    label.backgroundColor = .clear
    label.numberOfLines = 2
    label.font = UIFont(name: "Montserrat-Regular", size: 16.0)!
    label.textAlignment = .center
    label.textColor = .white
    label.text = "FIFA" + " \n " + "Europe 2018-2019"  
    self.navigationItem.titleView = label

干杯有美好的一天。

enter image description here


-5
投票

要在导航栏中设置多行大标题,请执行以下操作:

第1步:通过StoryBoard将标题添加到导航栏。

第2步:转到ViewController文件,并在viewDidLoad()方法下添加以下行

self.navigationController?.navigationBar.prefersLargeTitles = true 
© www.soinside.com 2019 - 2024. All rights reserved.