导航栏大标题-动画问题

Navigation Bar Large Title - Animation Issue

我在导航栏上使用大标题,当我点击一个单元格以转到下一个控制器时,大标题有一个奇怪的动画(如下面的 gif 所示)。它不会立即消失。

我尝试了以下解决方案但没有成功 (https://www.morningswiftui.com/blog/fix-large-title-animation-on-ios13)

我的代码:

在第一个视图控制器上:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        navigationItem.title = "New Order"
        navigationController?.navigationBar.prefersLargeTitles = true
}

在第二个视图控制器上(带有大标题):

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        navigationItem.title = "Menu"
        self.navigationController?.navigationBar.prefersLargeTitles = false
}

编辑:

Fabio 的回答是解决方案,但现在我遇到了另一个问题:

当我点击一个单元格时,导航栏的一部分是黑色的(如下所示)

尝试在第二个视图控制器中使用 viewDidLoad 而不是 viewWillAppear

将此扩展复制到设置导航栏:

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

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "YourTitle", preferredLargeTitle: true)

现在在 didSelectRow 中使用 pushVievController 调用第二个控制器,如下所示:

let controller = YourViewController()
    navigationController?.pushViewController(controller, animated: true)

在第二个控制器的 viewDidLoad 中将大标题设置为 false,如下所示:

navigationItem.largeTitleDisplayMode = .never

如果您想要浅色内容,请在 info.plist 中将 ViewController-based 状态栏设置为否。 如果你不想将 largeTitles 设置为 false 在 iOS 13 上测试,希望对您有所帮助 :) 现在你没事了:)

第二期: 在 SceneDelegate 中:var window: UIWindow? 在函数场景中放置这一行:

window?.backgroundColor = .yourColor

像这样:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let _ = (scene as? UIWindowScene) else { return }
    window?.backgroundColor = .white
}

设置你想要的颜色就这样:)

尝试在第一个视图控制器上插入:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationItem.title = "New Order"
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode =  .always
 }

在第二个视图控制器上:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationItem.title = "Menu"
    navigationItem.largeTitleDisplayMode = .never
}

所以你想要,你的第一个视图控制器有大标题,然后将下面的代码添加到你的第一个视图控制器中 viewWillAppear(animated:)

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

而且你想要,你的下一个视图控制器没有大标题,然后将下面的代码添加到你的下一个视图控制器中 viewWillAppear(animated:)

self.navigationItem.largeTitleDisplayMode = .never

结论是你只需要将 self.navigationController?.navigationBar.prefersLargeTitles 设置为 true 就好像你希望你的视图控制器需要在出现视图的开始时显示大标题