1

Im trying to create a custom delete action on a UITableView using the delegate method: tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

I set a transparent background color and an icon. I cant color the image no matter what i try. Is this possible?

@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:

This is what i managed to achieve till now

Edit: The tint(with:) function is from the CosmicMind/Material pod

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)
   }
}
AlexM
  • 552
  • 4
  • 17
  • 1
    Can you show up your `tint(with:)` function ? – Thomas Dec 13 '18 at 15:06
  • @matt Thanks a lot thats what i needed, i did search for a similar question but i did not come across that. Answer if you want here with a description of what could be the side effects and ill accept it. Otherwise ill flag it as duplicate. – AlexM Dec 14 '18 at 12:12
  • I tried this and its not working for me – beowulf Jan 19 '21 at 08:39

0 Answers0