ios - 弹出到非大半透明栏时 UINavigationBar 大标题问题

标签 ios swift uinavigationcontroller uinavigationbar ios-animations

我正在为两个 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. 版本

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

  1. 版本 == iOS 13

在弹出动画期间,大标题不会随关闭的 Controller 一起移动。视频在这里:https://youtu.be/25k3oz2_wcE

如何解决?提前谢谢你

最佳答案

最后我添加了

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

ControllerBViewDidLoad

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

ControllerAViewDidLoad 它按我的预期工作。

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

关于ios - 弹出到非大半透明栏时 UINavigationBar 大标题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392653/

相关文章:

iphone - 如何在iOS中从 View 顶部添加带有拖动动画的 subview ?

ios - 来自 dataWithContentsofURL 的 NSData 具有特定的键/值对 null

swift - 画一个带有纹理的圆形SKSpriteNode?

ios - 主题未显示在 firebase 控制台中

ios - 使用重复图像的纹理在 RealityKit 中可视化平面

ios - 如果使用 Cocoapods 和 use_frameworks!,我应该使用 Obj-C Bridging Header 吗?

ios - hidesBottomBarWhenPushed 在 iOS 7 中被忽略

ios - 通过导航 Controller 推送 Viewcontroller,显示黑屏

ios - 隐藏 UINavigationBar 但仍显示状态栏

ios - 如何在 Swift 中手动更改 UICollectionView 高度