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


11

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

Answers:


1

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

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.whiteI

// 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

5

我有一个自定义导航控制器安装在我的应用程序,该修改navigationBar小号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)

    }


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


2
不敢相信我们需要做的所有hack-fix来支持iOS 13:/感谢btw!
Sreejith,

奇怪,我没有将其设置为.none或者nil,我只是给它的颜色设置,出现后,它就可以工作
马克
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.