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

问题描述 投票:0回答:1

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

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:

从我的代码中获得的结果:

ios swift xcode uiimage uibarbuttonitem
1个回答
1
投票

您的图像背景必须是透明的,并且您可以将原始渲染模式始终设置为图像,以便显示如下所示的更改

let exportByEmail = UIBarButtonItem(image: exportImg.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(exportDataByEmail(_:)))
© www.soinside.com 2019 - 2024. All rights reserved.