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

时间:2018-03-22 22:49:11

标签: ios swift xcode uinavigationcontroller uinavigationbar

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

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)

代码在我的所有导航栏中生成此结果......

weird backbutton arrow

如您所见,细箭头与“后退”文本重叠,原始箭头仍然存在。我究竟做错了什么?如何删除原始箭头并将其替换为我的瘦自定义箭头?我仍然想保留文字。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

You need to modify the backIndicatorImage and backIndicatorTransitionMaskImage of the UINavigationBar class to replace the existing back indicator image with your custom one.

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