iOS tableView左滑刪除按鈕的自定義
本文僅做日常開(kāi)發(fā)記錄
遇到一個(gè)需求如下圖:

WechatIMG756.png
左滑刪除的時(shí)候,要?jiǎng)h除按鈕也有圓角
實(shí)現(xiàn)方式就是拿到刪除的那個(gè)視圖,然后修改layer,代碼如下:
如有小伙伴也需要,僅供參考,因?yàn)檫@樣寫(xiě),是不安全的,直接上代碼:
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TestCell
cell.backgroundColor = UIColor.white
if indexPath.row % 2 == 0 {
cell.contentView.backgroundColor = .systemYellow
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
90
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else {
return
}
//點(diǎn)擊刪除會(huì)觸發(fā)
print("____shan chu cell")
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "刪除"
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//guard let cell = self.feelCell else { return }
//cell.topLayer?.frame = cell.bgImageView.bounds
cell.contentView.layer.cornerRadius = 15
cell.contentView.layer.masksToBounds = true
let img = UIImageView()
img.contentMode = .scaleAspectFill
}
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: UIContextualAction.Style.normal, title: "刪除") { [weak self] _, _, complete in
print("___刪除了 cell")
complete(true)
}
//delete.image = UIImage(named: "aaa")
//delete.imageView!.contentMode = .scaleAspectFill
//delete.image.contentMode = .scaleAspectFill
delete.backgroundColor = UIColor.systemGreen
let action = UISwipeActionsConfiguration(actions: [delete])
action.performsFirstActionWithFullSwipe = false
return action
}
//滑動(dòng)刪除的時(shí)候會(huì)調(diào)用
func tableView(_ tab: UITableView, willBeginEditingRowAt: IndexPath) {
print("____開(kāi)始刪除")
test()
}
為刪除按鈕添加圓角
extension ViewController {
//iOS 11 和iOS 12獲取刪除按鈕的方法
private func test() {
for subView in tableView.subviews {
print("___sub:___\(subView)____\(subView.classForCoder)")
//_UITableViewCellSwipeContainerView
if NSStringFromClass(subView.classForCoder) == "_UITableViewCellSwipeContainerView"{
for sub in subView.subviews {
if NSStringFromClass(sub.classForCoder) == "UISwipeActionPullView" {
sub.backgroundColor = .clear
if let delBtn: UIButton = sub.subviews.first as? UIButton {
//此處可以拿到刪除按鈕后做一些相應(yīng)的操作
print(">>>完全自定義___\(delBtn)")
delBtn.isHidden = false
delBtn.layer.cornerRadius = 9.0
delBtn.clipsToBounds = true
}
sub.backgroundColor = .systemRed
sub.layer.cornerRadius = 9.0
sub.layer.masksToBounds = true
}
}
}
}
}
}
實(shí)現(xiàn)效果如下:

middle_img_v2_5bb43575-db6d-44cb-b278-a2c58b5ee04g.jpg
有需要的小伙伴,可以自取,親測(cè)沒(méi)問(wèn)題
ps:
iOS9、iOS11、iOS13具體獲取到對(duì)應(yīng)刪除按鈕的方式不同
iOS9、iOS11添加按鈕的方式有所不同
自定義的的控件的寬度存在一些差異