ios - 导航栏按钮图片

标签 ios swift uinavigationcontroller uinavigationbar

我正在使用此代码在我的导航栏上获取 Logo 。

override func viewDidAppear(animated: Bool) {

    let image = UIImage(named: "LogoWithTextSmaller.png")
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
    }

很好,但 Logo 没有任何颜色 - 除了“蓝色”。是不是因为它是一个png文件。有什么办法可以让它保留原来的颜色

我这样做了:

self.navigationItem.titleView = UIImageView(image: image)

这会以正确的颜色将图像带到导航栏上 - 但它位于中间,我希望它位于左侧。

最佳答案

您需要声明图像始终保持原始状态。所以添加如下代码

var image = UIImage(named: "image-name")
image = image?.withRenderingMode(.alwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: nil)

关于ios - 导航栏按钮图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299433/

相关文章:

ios - UITableView 问题,它在更新或插入新单元格后向上滚动一半

ios - Google Analytics(分析)增强型电子商务未收到收入

ios - 我可以在当前导航 Controller 中放置一个 UIView 吗?

ios - Swift UIScrollView - 仅垂直滚动的正确实现

iphone - 从 iOS 应用程序更新数据库

swift - CIColorControls 是否有不影响图像的设置?

ios - BarButtonItem 上的箭头

ios - 使用 UINavigationController 和 UINavigationBar 时图层的 y 位置动画错误

ios - 从 iOS Address Book API 查找常用联系人

ios - 如何在没有本地 Composer 的情况下使用 Fabric 框架将推文发布到 Twitter? iOS