14

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

4

2 回答 2

8

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

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

我最终做的是设置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)

    }


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

于 2019-10-02T10:35:28.660 回答
8

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

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
于 2019-10-14T13:23:43.013 回答