【问题标题】:How to set a Custom image programmatically to and UIBarButtonItem如何以编程方式将自定义图像设置为 UIBarButtonItem
【发布时间】:2019-04-04 22:09:42
【问题描述】:

我在将自定义图像分配给 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】:

    您的图片背景必须是透明的,并且您可以将图片始终设置为原始渲染模式,以保持原样显示,如下所示

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多