我正在其中一个应用程序的导航栏中添加大标题。问题是标题有点长,所以我需要在大标题中添加两行。如何在导航栏中添加两行大标题?
这与默认导航栏标题无关!这是关于 iOS 11 中引入的大标题。因此,请确保通过考虑大标题来添加建议。谢谢
最佳答案
基于@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)]
关于ios - 如何在导航栏中设置多行大标题? ( iOS 11 的新功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901318/