iOS11,UITableViewCell左滑之后在cell的layoutSubViews中已經(jīng)拿不到『UITableViewCellDeleteConfirmationView』,這時咱們可以通過以下方法來實現(xiàn)需求:
1、通過查看視圖層級發(fā)現(xiàn)
UISwipeActionPullView
TableView中出了這個『UISwipeActionPullView』
2、在tableView的代理方法『willbeginEditing』中調(diào)用setNeedsLayout,使當(dāng)前控制器的『viewWillLayoutSubviews』方法被調(diào)用(這一步必不可少,否則viewWillLayoutSubviews不會執(zhí)行的)
setNeedsLayout
3、關(guān)鍵代碼:
獲取UISwipeActionPullView
1,2兩種方式是OC中常用的class判斷的方式,樓主在這里耽誤了一點時間
經(jīng)嘗試后發(fā)現(xiàn) :3,4兩種方式可以完美的達到咱們的目的。
4、關(guān)鍵代碼:
UISwipeActionPullView的視圖層級
修改UISwipeActionStandardButton
5、修改完成:
修改完成!
6、iOS11以下的做法(可以在cell的layoutSubViews中,也可以在當(dāng)前VC中,這里寫的是第二種):
巧妙地在Indexpath初始化時,使row = -1 ,editIndexPath用來獲取被編輯的cell
7、完整代碼
完整代碼
8、CodeClean
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if editIndexPath.row < 0 { return }
let cell = tableview.cellForRow(at: editIndexPath)
let sup = UIDevice.current.systemVersion >= "11" ? tableview : cell!
let swipeStr = UIDevice.current.systemVersion >= "11" ? "UISwipeActionPullView" : "UITableViewCellDeleteConfirmationView"
let actionStr = UIDevice.current.systemVersion >= "11" ? "UISwipeActionStandardButton" : "_UITableViewCellActionButton"
for subview in sup.subviews {
if String(describing: subview).range(of: swipeStr) != nil {
for sub in subview.subviews {
if String(describing: sub).range(of: actionStr) != nil {
if let button = sub as? UIButton {
button.backgroundColor = .black
}
}
}
}
}
}