我正在为两个 UIViewController 实现 UINavigationBar:假设有 ControllerA 和 ControllerB。
ControllerA 具有具有 backgroundColor = .clear 属性的半透明 UINavigationBar。
ControllerB 具有 prefersLargeTitles 启用属性和白色背景。
我应该从 ControllerA -> ControllerB 推送和弹出。这是我在 controllerA 生命周期方法中实现的代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.backgroundColor = UIColor.clear
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.barTintColor = .clear
navigationController?.navigationBar.titleTextAttributes = [
.font: FontFamily.SFProRounded.bold.font(size: 18),
.foregroundColor: UIColor.white
]
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.largeTitleTextAttributes = [
.font: FontFamily.SFProRounded.bold.font(size: 22),
.foregroundColor: UIColor.black
]
}
navigationController?.navigationBar.titleTextAttributes = [
.font: FontFamily.SFProRounded.bold.font(size: 18),
.foregroundColor: UIColor.black
]
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.backgroundColor = UIColor.white
navigationController?.view.backgroundColor = UIColor.white
navigationController?.navigationBar.barTintColor = .white
}
下面我在不同 iOS 版本上遇到的问题:
- 版本
点击后退按钮以关闭 Controller 时动画和标题颜色不正确。视频在这里:https://youtu.be/1g9esUgYDK8
- 版本 == iOS 13
在弹出动画期间,大标题不会随关闭的 Controller 一起移动。视频在这里:https://youtu.be/25k3oz2_wcE
如何解决?提前谢谢你
最佳答案
最后我添加了
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .always
}
在 ControllerB 的 ViewDidLoad 和
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
在 ControllerA 的 ViewDidLoad 中 它按我的预期工作。
请注意,您只需设置一次prefersLargeTitles,最好是在打开应用程序时
关于ios - 弹出到非大半透明栏时 UINavigationBar 大标题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392653/