在 ios 13 中设置后退按钮箭头色调的正确方法是什么

What is the right way to set back button arrow tint in ios 13

在ios13,Apple引入了新的UINavigationBarAppearance代理对象来设置导航栏外观。除了一件小事,我已经能够设置几乎所有我需要的东西。后退按钮的箭头始终呈现蓝色色调,我不知道如何将其设置为我想要的颜色。我正在使用旧的 [[UINavigationBar appearance] setTintColor:] 方式,但我认为必须有一些方法可以使用 UINavigationBarAppearance 对象 API。有人知道怎么做吗?

我的应用程序中有一个自定义导航控制器设置,它会根据不同的场景修改 navigationBars titleTextAttributestintColor 和其他内容。

运行 iOS 13 上的应用 backBarButtonItem 箭头具有默认的蓝色色调。 视图调试器显示只有 UIBarButtonItems UIImageView 有这种蓝色。

我最后做的是设置 navigationBar.tintColor 两次以改变颜色...

public class MyNavigationController: UINavigationController, UINavigationControllerDelegate {

    public var preferredNavigationBarTintColor: UIColor?

    override public func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }


    public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

        // if you want to change color, you have to set it twice
        viewController.navigationController?.navigationBar.tintColor = .none
        viewController.navigationController?.navigationBar.tintColor = preferredNavigationBarTintColor ?? .white

        // following line removes the text from back button
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

    }


寻找解决方案时最奇怪的部分是结果不一致,这让我认为它与视图生命周期 and/or 外观动画或 Xcode 缓存有关:)

设置外观(代理)后退按钮颜色的新方法是:

let appearance = UINavigationBarAppearance()

// Apply the configuration option of your choice
appearance.configureWithTransparentBackground()
            
// Create button appearance, with the custom color
let buttonAppearance = UIBarButtonItemAppearance(style: .plain)
buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]

// Apply button appearance
appearance.buttonAppearance = buttonAppearance

// Apply tint to the back arrow "chevron"
UINavigationBar.appearance().tintColor = UIColor.white

// Apply proxy
UINavigationBar.appearance().standardAppearance = appearance

// Perhaps you'd want to set these as well depending on your design:
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance