As I was using tableView:editActionsForRowAt: to provide some useful functionalities in an iOS app.
func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
print(#function)
.... useful code .....
}
I got to the point where some other API similar to the one above, but fired on a right swipe, rather than left swipe like tableView:editActionsForRowAt: would have been useful. Say something like:
func tableView(_ tableView: UITableView,
editMoreActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
print(#function)
.... useful code .....
}
Browsing the net I found those two methods of the UITableViewDelegate protocol:
tableView:leadingSwipeActionsConfigurationForRowAt
tableView:trailingSwipeActionsConfigurationForRowAt
After some basic experimentation, I thought I could get what I wanted, using tableView:leadingSwipeActionsConfigurationForRowAt, so providing some more functionalities to the user on the right swipe of a cell. But it seems like this method doesn't give much freedom. For example, here the code I have (very much inspired by some sample I found on the internet):
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt
indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let modifyAction = UIContextualAction(style: .normal, title: nil, handler: {
(ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
modifyAction.image = UIImage(named: "MyNiceIcon")!.withColor(color: .black)
modifyAction.backgroundColor = .clear
return UISwipeActionsConfiguration(actions: [modifyAction])
}
Here even though these lines are present:
modifyAction.image = UIImage(named: "MyNiceIcon")!.withColor(color: .black)
modifyAction.backgroundColor = .clear
MyNiceIcon stays white and the background of the button is gray.
Am I missing something? Or can the color of the image used only be white? And the background color never transparent?
I hope this will help you. You can set the background color of any swipe action plus you can even create a few of them on one swipe. Simple example would be using something like this:
This code comes from one of my projects and allows me to set a delete action from the right. If you want an action to come from the left simply use the same function but with
leadingSwipeActionConfigurationForRowAtwhere you return the UISwipeActionsConfiguration, you use an array of UIContextualAction elements.