Ios 如何以编程方式将自定义图像设置为和UIBarButtonim
我在为UIBarButtonItem指定自定义图像时遇到问题,主要问题是创建按钮时图像显示为白色正方形。 这是我的密码:Ios 如何以编程方式将自定义图像设置为和UIBarButtonim,ios,swift,xcode,uiimage,uibarbuttonitem,Ios,Swift,Xcode,Uiimage,Uibarbuttonitem,我在为UIBarButtonItem指定自定义图像时遇到问题,主要问题是创建按钮时图像显示为白色正方形。 这是我的密码: fileprivate func configureNavigationBar() { tabBarController?.navigationItem.title = lot.name let exportImg: UIImage = UIImage(named: "action.png")! let addButton =
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,图像位于从我的资产添加的变量exportImg中:
从我的代码中获得的结果:
您的图像背景必须是透明的,并且您可以将“始终原始渲染模式”设置为“图像”,以便在不进行更改的情况下显示,如下所示
let exportByEmail = UIBarButtonItem(image: exportImg.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(exportDataByEmail(_:)))