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

Rom*_*man 8 uinavigationbar ios13 uinavigationbarappearance

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

Jus*_*zer 14

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

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
Run Code Online (Sandbox Code Playgroud)


Art*_*hur 7

我有一个自定义导航控制器安装在我的应用程序,该修改navigationBar小号titleTextAttributestintColor和其他人根据不同的场景。

在 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)

    }


Run Code Online (Sandbox Code Playgroud)

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

  • 不敢相信我们为了支持 iOS 13 需要进行的所有黑客修复:/顺便说一句,感谢您的修复! (2认同)