iOS 左滑按钮(UITableViewRowAction)显示图片
问题
我们想要左滑删除按钮显示图片。但是系统默认只能添加文字。
解决办法
1、 找到UITableViewRowAction里面的字view,里面有一个button,我们加的文字就加在上面。
2、将图片设置为背景颜色
https://stackoverflow.com/questions/29421894/uitableviewrowaction-image-for-title
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: title) { (action, indexpath) in
}
deleteAction.backgroundColor = UIColor(patternImage: UIImage(named: "IMAGE_NAME"))
return [deleteAction]
}
3、iOS 11以上 UITableView已经有了这个代理方法。
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .normal, title: "删除") { _, index in
}
delete.backgroundColor = UIColor(valueRGB: 0xF2463D, alpha: 1.0)
return [delete]
}
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: nil, handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
debugPrint("Delete tapped")
success(true)
})
deleteAction.image = UIImage(named: "integral_icon_delect")
deleteAction.backgroundColor = UIColor(valueRGB: 0xF2463D, alpha: 1.0)
return UISwipeActionsConfiguration(actions: [deleteAction])
}