I made a custom UITableViewRowAction. Now I'd like to add an image instead of the text. I know that it's possible but don't know how to do it. Does someone of you knows how to do this in Swift and would like to help me? Thanks for your answers!
Apple introduced flexible way to declare row actions with great benefits.
extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let askAction = UIContextualAction(style: .normal, title: nil) { action, view, complete in print("Ask!") complete(true) } // here set your image and background color askAction.image = IMAGE askAction.backgroundColor = .darkGray let blockAction = UIContextualAction(style: .destructive, title: "Block") { action, view, complete in print("Block") complete(true) } return UISwipeActionsConfiguration(actions: [blockAction, askAction]) } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cell.textLabel?.text = "row: \(indexPath.row)" } }
Example:
You need to set UIImage to backgroundColor of row action, concretely by:
UIColor(patternImage: UIImage(named: "IMAGE_NAME"))
[UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE_NAME"]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With