弹出到 non-large 半透明栏时 UINavigationBar 大标题出现问题
Issue with UINavigationBar large title while popping to non-large translucent bar
我正在为两个 UIViewController 实现 UINavigationBar:假设 ControllerA 和 ControllerB。
ControllerA 有半透明的 UINavigationBar with backgroundColor = .clear 属性.
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 版本上遇到的问题:
- 版本 < iOS 13
点按返回按钮以关闭控制器时动画和标题颜色不正确。视频在这里:https://youtu.be/1g9esUgYDK8
- 版本==iOS13
在弹出动画期间,大标题不会随关闭的控制器一起移动。视频在这里:https://youtu.be/25k3oz2_wcE
如何解决?提前谢谢你
最后我添加了
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .always
}
在 ViewDidLoad 中 ControllerB 和
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
在 ViewDidLoad 中 ControllerA
它按我的预期工作。
注意只需要设置一次prefersLargeTitles,最好在打开应用的时候设置
我正在为两个 UIViewController 实现 UINavigationBar:假设 ControllerA 和 ControllerB。
ControllerA 有半透明的 UINavigationBar with backgroundColor = .clear 属性.
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 版本上遇到的问题:
- 版本 < iOS 13
点按返回按钮以关闭控制器时动画和标题颜色不正确。视频在这里:https://youtu.be/1g9esUgYDK8
- 版本==iOS13
在弹出动画期间,大标题不会随关闭的控制器一起移动。视频在这里:https://youtu.be/25k3oz2_wcE
如何解决?提前谢谢你
最后我添加了
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .always
}
在 ViewDidLoad 中 ControllerB 和
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
在 ViewDidLoad 中 ControllerA 它按我的预期工作。
注意只需要设置一次prefersLargeTitles,最好在打开应用的时候设置