目前,我为导航栏启用了大标题,并在ViewController的viewdidLoad中添加了以下代码:
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd"
let result = formatter.string(from: date)
self.title = “This is a Test\n\(result)"
var count = 0
for item in(self.navigationController?.navigationBar.subviews)! {
for sub in item.subviews{
if sub is UILabel{
if count == 1 {
break;
}
let titleLab :UILabel = sub as! UILabel
titleLab.numberOfLines = 0
titleLab.text = self.title
titleLab.lineBreakMode = .byWordWrapping
count = count + 1
}
}
}
self.navigationController?.navigationBar.layoutSubviews()
self.navigationController?.navigationBar.layoutIfNeeded()
当导航栏在不再变大的“正常状态”下折叠时,如何显示完全不同的标题?
答案 0 :(得分:1)
您可以 observe
的{{1}}的{{1}} ,并通过选中bounds
的{{1}}来更改navigationBar
title
。
1。(小标题),{{1}中的height
, = 44
2。(对于大标题,{{1}中的navigationBar
,<{>>>> 44
height
答案 1 :(得分:0)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let heightForCollapsedNav = UINavigationController().navigationBar.frame.size.height
let navHeight = navigationController!.navigationBar.frame.size.height
navigationController?.navigationBar.topItem?.title = navHeight <= heightForCollapsedNav ? "Collapsed" : "Large"
}