问题
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?
回答1:
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:
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = deleteAction(at: indexPath)
return UISwipeActionsConfiguration(actions: [delete])
}
@available(iOS 11.0, *)
private func deleteAction(at indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion) in
let confirmDeletionAC = UIAlertController(title: "Delete", message: "Are you sure you want to delete this record?", preferredStyle: .alert)
let confirmAlertAction = UIAlertAction(title: "Delete", style: .default, handler: { (action) in
self.deleteObject(at: indexPath)
self.historicTableView.deleteRows(at: [indexPath], with: .automatic)
// print("SWIPED TO DELETE")
completion(true)
})
let cancelAlertAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
// print("DELETE CANCELLED")
completion(false)
})
confirmDeletionAC.addAction(cancelAlertAction)
confirmDeletionAC.addAction(confirmAlertAction)
self.present(confirmDeletionAC, animated: true)
}
action.backgroundColor = UIColor.flatWatermelon
return action
}
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 leadingSwipeActionConfigurationForRowAt
where you return the UISwipeActionsConfiguration, you use an array of UIContextualAction elements.
回答2:
To set original image in action you can create custom class of UIImage
and override method withRenderingMode
as follow
class OriginalImageRender: UIImage {
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
return self
}
}
To use:
if let cgImageX = UIImage(named: "MyNiceIcon")?.cgImage {
modifyAction.image = OriginalImageRender(cgImage: cgImageX, scale: UIScreen.main.nativeScale, orientation: .up)
}
Above code will display image as original which added inside image assets.
回答3:
1- just add this class at the end of your class
class OriginalImageRender: UIImage {
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
return self
}
} 2- create a cgImage from your UIImage
let cgImageX = UIImage(named: "delete")?.cgImage
3- pass this cgImage to class OriginalImageRender and add this image to your action
action.image = OriginalImageRender(cgImage: cgImageX!)
It Worked for me, Hope helpful to you
来源:https://stackoverflow.com/questions/57864748/tablevieweditactionsforrowat-on-right-swipe