我有一个视图控制器,我需要在导航栏上显示多行标题。为此,我写了这样一个协议------------------------。
import UIKit
protocol CustomNavigationBar {
func setupNavigationMultilineTitle(titleText: String, prefersLargeTitles: Bool, largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode)
}
然后将其延伸--
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()
}
}
}
}
}
}
在我的视图控制器中,我符合这个协议,在viewDidAppear方法里面,我调用了setupNavigationMultilineTitle方法,如下所示----------。
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")
}
**
这在运行小于iOS13的iPhone上效果不错。
**
**
然而,在运行大于iOS 13的iPhone上,它只是显示一行,然后截断。
**
iOS13中的UINavigationBar有什么变化吗?我研究了一下,发现了一些关于背景颜色的东西,但是没有任何关于使用prefersLargeTitles和largeTitleDisplayMode的多行标题的内容。
谁能帮我在iOS13上把这个弄起来?
谢谢!