iOS 左滑按钮(UITableViewRowAction)显示图片

版权声明:转载请标明出处 https://blog.csdn.net/ZY_FlyWay/article/details/86134740

问题

我们想要左滑删除按钮显示图片。但是系统默认只能添加文字。


解决办法

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])
    }

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2gi5aqk2msg08

猜你喜欢

转载自blog.csdn.net/ZY_FlyWay/article/details/86134740
今日推荐