iOS14 navigationItem.largeTitleDisplayMode =.始终不起作用 [英] iOS14 navigationItem.largeTitleDisplayMode = .always not work

查看:307
本文介绍了iOS14 navigationItem.largeTitleDisplayMode =.始终不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ViewController and a DetailViewController, in the ViewDidLoad of the ViewController I set the following code, the purpose is to make the ViewController always use the large title

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

DetailViewControllerViewDidLoad中设置以下代码,目的是使DetailViewController不使用大标题

In the ViewDidLoad of the DetailViewController I set the following code, the purpose is to make the DetailViewController not use the large title

navigationItem.largeTitleDisplayMode = .never

当我从DetailViewController返回到ViewController时,将显示小标题,而不是ViewController中的大标题.此代码在iOS12和iOS13中正确.如何使ViewController始终在iOS14上显示大标题?

When I return from DetailViewController to ViewController, the small title is displayed instead of the large title in ViewController. This code is correct in iOS12 and iOS13. How to make the ViewController always display the large title on iOS14?

当前在App Store中使用Xcode12

Currently using Xcode12 from the App Store

推荐答案

使用我的扩展名:

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中设置导航栏:

set your navigation bar in viewWillAppear:

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

现在校准显示详细控制器的功能:

now cal the function that present your detail controller:

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

这是结果:

这篇关于iOS14 navigationItem.largeTitleDisplayMode =.始终不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆