ios - 如何在应用程序委托(delegate)文件中的整个应用程序中设置自定义导航 Controller 后退箭头图像?

标签 ios swift xcode uinavigationcontroller uinavigationbar

我想更改整个应用程序中所有导航 Controller 中的所有后退箭头。我想避免在每个文件中手动更改图像,因为那会很耗时。我将在我的 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)

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

weird backbutton arrow

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

最佳答案

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

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

关于ios - 如何在应用程序委托(delegate)文件中的整个应用程序中设置自定义导航 Controller 后退箭头图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439680/

相关文章:

ios - Swift:iAd UITableViewController 位置

ios - 将 NSAttributedString 转换成 Data 进行存储

在 XCode 中定义静态类变量时出现 C++ 重复符号错误

ios - 如何从 xcode 卸载 swiftlint,或关闭它的警告

CocoaPods Google Maps SDK 上的 iOS 单元测试失败

ios - 增加 iPad 上的 UIAlertView 大小

c++ - 尝试从 Swift 调用 C++ 代码时出现链接器错误

swift - 如何使我的排序功能真正在表格 View 中显示排序后的书籍

objective-c - Xcode 错误 : Expected identifier

ios - 使用 NSEqualToPredicateOperatorType 时如何使谓词忽略小数点?