大标题折叠时如何显示不同的导航标题?

时间:2019-06-12 05:41:36

标签: swift uinavigationbar

目前,我为导航栏启用了大标题,并在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()

当导航栏在不再变大的“正常状态”下折叠时,如何显示完全不同的标题?

2 个答案:

答案 0 :(得分:1)

您可以 observe的{​​{1}}的{​​{1}} ,并通过选中bounds的{​​{1}}来更改navigationBar title

1。小标题),{{1}中的height = 44

2。(对于大标题,{{1}中的navigationBar,<{>>>> 44

height

enter image description here

答案 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"
}