iOS14 navigationItem.largeTitleDisplayMode = .always 不工作

BaQ*_*iWL 11 uinavigationbar uinavigationcontroller ios ios14

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

self.navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
Run Code Online (Sandbox Code Playgroud)

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

navigationItem.largeTitleDisplayMode = .never
Run Code Online (Sandbox Code Playgroud)

当我从DetailViewControllerto返回时ViewController,显示的是小标题而不是 中的大标题ViewController。此代码在 iOS12 和 iOS13 中是正确的。如何让ViewControlleriOS14 上总是显示大标题?

目前使用 App Store 中的 Xcode12

Lal*_*hna 10

最后解决了这个问题。

边缘情况:

如果您使用大标题,并且同一视图控制器上有多个滚动视图。导航栏将侦听 UIScrollView 类型的子视图(直接子视图)上的滚动操作。

解决方案

您必须防止当前视图控制器的大标题折叠功能。

其概念与@BaQiWL 提到的相同。view.addSubview(UIView())但如果您使用情节提要,则不仅仅添加可以解决这个问题。

为此,您必须将视图添加为 Viewcontroller 的第一个子视图。(view.sendSubviewToBack有窍门)。

// Call this method on `viewDidLoad`
private func preventLargeTitleCollapsing() {
    let dummyView = UIView()
    view.addSubview(dummyView)
    view.sendSubviewToBack(dummyView)
}
Run Code Online (Sandbox Code Playgroud)

或者通过故事板:

在此输入图像描述


Sym*_*mon 8

对于 iOS 14,需要添加 sizeToFit 函数。下面的代码总是有效。

navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.sizeToFit()
Run Code Online (Sandbox Code Playgroud)

  • 它不适合我。我刚刚在 VC-1 的 didload 和 willappear 上调用了它。 (2认同)

Fab*_*bio -3

使用我的扩展:

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
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

在 viewWillAppear 中设置导航栏:

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

现在计算显示您的详细控制器的函数:

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

这是结果:

在此输入图像描述

在此输入图像描述