• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

iOS14 navigationItem.largeTitleDisplayMode = .always 不起作用

iOS cps12345 1年前 (2020-11-07) 506次浏览 0个评论

我有一个ViewController和一个DetailViewController,在ViewDidLoad中ViewController我设置了以下代码,目的是使ViewController始终使用大标题

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

在ViewDidLoad中的DetailViewController我设置下面的代码,目的是使DetailViewController不使用的大标题

navigationItem.largeTitleDisplayMode = .never

当我从返回DetailViewController时ViewController,显示的是小标题,而不是中的大标题ViewController。此代码在iOS12和iOS13中正确

 

解决

使用下面的扩展:

extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithOpaqueBackground()
    navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
    navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
    navBarAppearance.backgroundColor = backgoundColor

    navigationController?.navigationBar.standardAppearance = navBarAppearance
    navigationController?.navigationBar.compactAppearance = navBarAppearance
    navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

    navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
    navigationController?.navigationBar.isTranslucent = false
    navigationController?.navigationBar.tintColor = tintColor
    navigationItem.title = title

} else {
    // Fallback on earlier versions
    navigationController?.navigationBar.barTintColor = backgoundColor
    navigationController?.navigationBar.tintColor = tintColor
    navigationController?.navigationBar.isTranslucent = false
    navigationItem.title = title
  }
 }
}

在viewWillAppear设置navigationBar:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    configureNavigationBar(largeTitleColor: .white, backgoundColor: .yourColor, tintColor: .white, title: "YourTitle", preferredLargeTitle: true)
}

调用显示:

@objc func DetailController(){
    let controller = DetailViewController()
    controller.navigationItem.largeTitleDisplayMode = .never
    navigationController?.pushViewController(controller, animated: true)
}
喜欢 (0)

您必须 登录 才能发表评论!