Swift-UITableView編輯模式自定義右側拖拽標簽

如圖 右側拖拽圖標自定義操作
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()
    }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容