更改导航栏时平滑过渡 "prefersLargeTitles"

Smooth transition when changing navigation bar "prefersLargeTitles"

我有一个 pushed 到导航堆栈的视图控制器。堆栈有 navigationBar.prefersLargeTitles = true,而这个新的视图控制器有 navigationBar.prefersLargeTitles = false。我在推送到堆栈的视图控制器中使用以下代码实现了这一点:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.navigationBar.prefersLargeTitles = false
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.navigationBar.prefersLargeTitles = true
}

但是,当我 return 返回呈现视图控制器时,导航栏从 navigationBar.prefersLargeTitles = falsenavigationBar.prefersLargeTitles = true 的变化有点小问题。有什么办法可以让它更顺畅吗?

非常感谢

与其直接通过导航控制器更改首选项,不如通过您想要影响的特定视图控制器的导航项更改行为。

// Root UIViewController
class ViewControllerA: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

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

// Pushed UIViewController
class ViewControllerB: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        navigationItem.largeTitleDisplayMode = .never
    }
}

您可以删除 viewWillAppearviewWillDisappear 中的行。