如圖 右側拖拽圖標自定義操作

1640743847535.jpg
原理就是遍歷去找出這個imageView,修改它。
方法1:代理方法內處理
// 改系統(tǒng)cell拖動圖標
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if tableView.isEditing {
cell.subviews.forEach { view in
if view.className().range(of: "UITableViewCellReorderControl", options: .caseInsensitive) != nil {
view.subviews.forEach { subView in
if subView.className() == UIImageView().className() {
let imgView = subView as! UIImageView
imgView.image = UIImage(named: "list_icon_drag_20pt")
imgView.contentMode = .center
}
}
}
}
}
}
方法2:tableViewCell內處理
func customDragImgIcon() {
// UITableViewCellReorderControl
for view in subviews where view.description.contains("Reorder") {
for case let subview as UIImageView in view.subviews {
subview.image = UIImage(named: "list_icon_drag_20pt")
subview.contentMode = .center
}
}
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
customDragImgIcon()
}
iOS15系統(tǒng)發(fā)現(xiàn),使用以上方法,開始拖動時圖標會變回系統(tǒng)樣式
開始在以下這兩個方法去遍歷修改,發(fā)現(xiàn)沒效果
override func willTransition(to state: UITableViewCell.StateMask) {
super.willTransition(to: state)
}
override func didTransition(to state: UITableViewCell.StateMask) {
super.didTransition(to: state)
}
解決方法:tableViewcell layoutSubviews處理
override func layoutSubviews() {
super.layoutSubviews()
customDragImgIcon()
}