如何在应用程序委托文件中的整个应用程序中设置自定义导航控制器后退箭头图像?

How to set custom navigation controller back arrow image throughout app in the app delegate file?

我想更改整个应用程序中所有导航控制器中的所有后退箭头。我想避免在每个文件中手动更改图像,因为那会很耗时。我将 post 我目前在 AppDelegate 文件中的 didFinishLaunchingWithOptions 方法中的代码。

let backButton = UIImage(named: "backbutton-thin")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 16, 16))
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 5, topCapHeight: 0)
barButtonAppearance.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)

let item = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationController.self])
item.setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
item.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)

代码在我所有的导航栏中产生了这个结果...

如您所见,细箭头与 "Back" 文本重叠,原始箭头仍然存在。我究竟做错了什么?如何删除原始箭头并用我的细自定义箭头替换它?不过我还是想保留文字。任何帮助将不胜感激。

您需要修改 UINavigationBar class 的 backIndicatorImagebackIndicatorTransitionMaskImage 以将现有的后退指示器图像替换为您的自定义图像。

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "backbutton-thin")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "backbutton-thin")