[英]Smooth transition when changing navigation bar "prefersLargeTitles"
I have a view controller that is pushed
onto a navigation stack.我有一个
pushed
到导航堆栈上的视图控制器。 The stack has navigationBar.prefersLargeTitles = true
, whilst this new view controller has navigationBar.prefersLargeTitles = false
.堆栈具有
navigationBar.prefersLargeTitles = true
,而这个新的视图控制器具有navigationBar.prefersLargeTitles = false
。 I achieve this using the following code in the view controller that is pushed onto the stack:我在推入堆栈的视图控制器中使用以下代码实现了这一点:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
However, when I return back to the presenting view controller, the change in the navigation bar from navigationBar.prefersLargeTitles = false
to navigationBar.prefersLargeTitles = true
is a bit glitchy.但是,当我返回到呈现视图控制器时,导航栏从
navigationBar.prefersLargeTitles = false
更改为navigationBar.prefersLargeTitles = true
有点小问题。 Is there any way to make this smoother?有没有办法让这个更顺畅?
Many thanks非常感谢
Instead of directly changing the preference via the navigation controller, you should change the behavior via the navigation item of the specific view controller you would like to affect.您应该通过您想要影响的特定视图控制器的导航项来更改行为,而不是通过导航控制器直接更改首选项。
// 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
}
}
You can remove the lines you have in viewWillAppear
and viewWillDisappear
.您可以删除
viewWillAppear
和viewWillDisappear
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.