是否可以为UIContextualAction对象的图像着色?

时间:2018-12-13 14:46:21

标签: ios swift uitableview uikit

我正在尝试使用委托方法在UITableView上创建自定义删除操作: tableView(_ tableView:UITableView,trailingSwipeActionsConfigurationForRowAt indexPath:IndexPath)-> UISwipeActionsConfiguration? < / p>

我设置了透明的背景色和图标。无论尝试什么,我都无法为图像着色。这可能吗?

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let delete = UIContextualAction(style: .destructive, title: nil, handler: { contextualAction, view, _ in

        if self.inboxMessages.count > 0 {
            self.inboxMessages[indexPath.row].delete(onCompletion: {success in

                if success{

                    NSLog("Message deleted successfully!")

                    if self.inboxMessages.indices.contains(indexPath.row) {
                        self.inboxMessages.remove(at: indexPath.row)
                    }

                    DispatchQueue.main.sync {

                        if indexPath.row != 0 {
                            self.tableView.deleteRows(at: [indexPath], with: .fade)
                        }

                        self.tableView.reloadData()
                    }
                }
            })
        }
    })

    delete.image = UIImage(named: "deleteInboxIcon")?.withRenderingMode(.alwaysTemplate).tint(with: .red)

    let color = UIColor(white: 0, alpha: 0.000001)
    delete.backgroundColor = color
    //        delete.backgroundColor = UIColor(patternImage: UIImage(named: "DefaultInboxIcon")!)

    let actionsConfig = UISwipeActionsConfiguration(actions: [delete])
    actionsConfig.performsFirstActionWithFullSwipe = true

    return actionsConfig
}

这是我到目前为止设法实现的目标:

This is what i managed to achieve till now

编辑:tint(with:)功能来自CosmicMind /物料吊舱

extension UIImage {

   open func tint(with color: UIColor) -> UIImage? {

       UIGraphicsBeginImageContextWithOptions(size, false, Screen.scale)

       guard let context = UIGraphicsGetCurrentContext() else {
          return nil
       }

       context.scaleBy(x: 1.0, y: -1.0)
       context.translateBy(x: 0.0, y: -size.height)

       context.setBlendMode(.multiply)

       let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
       context.clip(to: rect, mask: cgImage!)
       color.setFill()
       context.fill(rect)

       let image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
       return image?.withRenderingMode(.alwaysOriginal)
   }
}

0 个答案:

没有答案