【问题标题】:Issue with UINavigationBar large title while popping to non-large translucent bar弹出到非大半透明栏时 UINavigationBar 大标题出现问题
【发布时间】:2020-02-12 00:22:18
【问题描述】:

我正在为两个 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 版本上遇到的问题:

  1. 版本

点击返回按钮以关闭控制器时动画和标题颜色不正确。视频在这里:https://youtu.be/1g9esUgYDK8

  1. 版本 == iOS 13

在弹出动画期间,大标题不会随着被解散的控制器移动。视频在这里:https://youtu.be/25k3oz2_wcE

如何解决?提前谢谢你

【问题讨论】:

  • 覆盖 B 类中的 viewWillDisapper() 并再次在那里设置普通导航栏。它有助于解决不正确的动画。
  • 它不能解决问题
  • 我之前也遇到过同样的问题,但我已经完成了上述更改并解决了。
  • 我试过了,但不幸的是它没有帮助..看起来我在两个 UINavigationBar 状态之间进行转换完全错误
  • 您可以尝试在ControllerB 中覆盖viewWillDisappear 并为ControllerA 设置导航更改。从viewWillAppear 中删除导航设置并将其设置为viewDidLoad

标签: ios swift uinavigationcontroller uinavigationbar ios-animations


【解决方案1】:

我终于添加了

if #available(iOS 11.0, *) {
    navigationItem.largeTitleDisplayMode = .always
}

ControllerB

ViewDidLoad
if #available(iOS 11.0, *) {
    navigationItem.largeTitleDisplayMode = .never
}

ControllerAViewDidLoad 中 它按我的预期工作。

注意prefersLargeTitles只需要设置一次,最好是在打开应用程序时

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-17
    • 2013-12-27
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多