swift - 更改 UINavigationBar 按钮图像

标签 swift uiimage uinavigationbar uinavigationitem

我正在尝试替换点击右侧导航栏按钮时显示的图像。

目前似乎正在发生的事情是它没有替换按钮,而是在旧按钮仍然存在的情况下尝试设置它。如您所见,这会导致奇怪的效果:

enter image description here

这是一张指南针图像,所以应该只有一个圆圈,但正如您所见,出现了三个圆圈。

这是我的代码:

let backImg: UIImage = UIImage(named: "Compass.png")!
navButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

有没有办法删除现有的并用新的替换它?

谢谢。

最佳答案

听起来您可能正在设置 UIBarButtonItem 的 backgroundImage,而您真正想要设置的是它的 image。因此,您最终会同时看到它们。

关于swift - 更改 UINavigationBar 按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29658484/

相关文章:

swift - Nib View 与模拟器不同

swift - Thread 1 : EXC_BAD_ACCESS (code=2, address=0x10cb21d98) while running the code

ios - 在 iOS 中考虑 alpha 部分在另一个图像上添加图像

ios - CGContextRestoreGState : invalid context 0x0 xcode error

ios - 尝试将图像大小调整为缩略图,kCGInterpolationHigh 出现错误 "Use of unresolved identifier"

swift - 在数据库中实现[用户-订阅/订阅者]关系的最佳方法

ios - UINavigationbar barTintColor 在 UISplitview 模板中不起作用

ios - 如何删除导航 Controller 后退按钮标题?

ios - 带标题的 UITabBarController header

swift - 'Type Body Length' 在 Swiftlint 中到底意味着什么?