ios - 如何以编程方式将自定义图像设置为 UIBarButtonItem

标签 ios swift xcode uiimage uibarbuttonitem

我在将自定义图像分配给 UIBarButtonItem 时遇到问题,主要问题是在创建按钮时图像显示为白色方 block 。 这是我的代码:

fileprivate func configureNavigationBar() {
        tabBarController?.navigationItem.title = lot.name
        let exportImg: UIImage = UIImage(named: "action.png")!
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(showCreationView(_:)))
        let exportByEmail = UIBarButtonItem(image: exportImg, style: .done, target: self, action: #selector(exportDataByEmail(_:)))
        tabBarController?.navigationItem.rightBarButtonItems = [exportByEmail,addButton]
    }

问题出在 exportByEmail,图片在从我的 Assets 添加的变量 exportImg 中:

image1

从我的代码中得到的结果: image2

最佳答案

图片的背景必须是透明的,可以设置always original rendering mode为image,原样显示如下

let exportByEmail = UIBarButtonItem(image: exportImg.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(exportDataByEmail(_:)))

关于ios - 如何以编程方式将自定义图像设置为 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525634/

相关文章:

iphone - iphone 中的分组 TableView

ios - 如何防止 UIaccessibility 对在 viewforheaderinsection 中创建的控件说 "heading"?

swift - iOS - Swift 3 - 蓝牙背景

ios - 如何指示用户滑动的方向,而不是 Swift 中的下一个和上一个 View Controller ?

iOS - UITableViewCell 使文本加粗

ios - UITabBar 中的选择字段自定义

ios - 我如何确保没有重复的请求从 cellForRowAtIndexPath 中的 url 获取图像以用于 iOS 中的 UITableView

iOS:-dynamic 未指定以下标志无效:-sectcreate

ios - “字节”不可用 : use withUnsafeBytes instead

swift - 如何在 NSRange 中转换范围?