Swift UITableView左滑刪除按鈕圖文上下排列

一哮独、需求:

需要左滑刪除按鈕需要圖文上下顯示(UIBtutton的圖文顯示庐橙,可根據(jù)自己需求顯示)

左滑刪除按鈕.jpeg
二、先兩種情況

1借嗽、iOS11.0之前的系統(tǒng)版本
2态鳖、iOS11.0之后的系統(tǒng)版本(注意iOS13.0之后也有變化)

三、iOS11.0之前的系統(tǒng)版本代碼如下

3.1UITableView 實現(xiàn)editActionsForRowAt代理方法
// 適配 iOS11.0之前
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        if #available(iOS 11.0, *) {
            
        } else {
            let deleteAction = UITableViewRowAction(style: .destructive, title: "取消收藏") { (action, indexPath) in
                // TODO
            }
            
            let shareAction = UITableViewRowAction(style: .normal, title: "分享") { (action, indexPath) in
                // TODO
            }
            shareAction.backgroundColor =  MSFColor.RGBA(red: 255, green: 153, blue: 0, alpha: 1.0)
            return [deleteAction, shareAction]
        }
        return nil
    }
3.2修改側(cè)滑按鈕恶导,在自定義cell中重寫didTransition函數(shù)
// 適配 iOS11.0之前
    override func didTransition(to state: UITableViewCell.StateMask) {
        if state == .showingDeleteConfirmation {
            for subView in self.subviews {
                if NSStringFromClass(subView.classForCoder) == "UITableViewCellDeleteConfirmationView" {
                    if let deleteBtn: UIButton = subView.subviews.first as? UIButton  {
                        changeAction(sourceBtn: deleteBtn, title: "取消收藏", imageStr: "icon_collecton_delete")
                    }
                    if let shareBtn: UIButton = subView.subviews.last as? UIButton  {
                        changeAction(sourceBtn: shareBtn, title: "分享", imageStr: "icon_collecton_share")
                    }
                }
            }
        }
    }
    
    private func changeAction(sourceBtn: UIButton, title: String?, imageStr: String?) {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: sourceBtn.frame.width, height: sourceBtn.frame.height)
        btn.backgroundColor = sourceBtn.backgroundColor
        btn.setImage(UIImage(named: imageStr ?? ""), for: .normal)
        btn.setTitle(title, for: .normal)
        btn.titleLabel?.font = MSFFont.fontValue(size: 14, weight: .regular)
        btn.setTitleColor(UIColor.white, for: .normal)
        // 修改button的圖文上下顯示
        btn.updateBtnEdgeInsets(style: .Top, space: 1)
        btn.isUserInteractionEnabled = false
        sourceBtn.backgroundColor = .clear
        sourceBtn.addSubview(btn)
    }
四浆竭、iOS11.0之后的系統(tǒng)版本代碼如下

4.1UITableView 實現(xiàn)trailingSwipeActionsConfigurationForRowAt代理方法
@available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .normal, title: "") { [weak self] (action, view, resultClosure) in
            guard let `self` = self else {
                return
            }
            // TODO
        }
        let shareAction = UIContextualAction(style: .normal, title: "") { [weak self] (action, view, resultClosure) in
            guard let `self` = self else {
                return
            }
            // TODO
        }
        deleteAction.backgroundColor = .red
        shareAction.backgroundColor = MSFColor.RGBA(red: 255, green: 153, blue: 0, alpha: 1.0)
        let actions = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
        actions.performsFirstActionWithFullSwipe = false; // 禁止側(cè)滑到最左邊觸發(fā)刪除或者分享回調(diào)事件
        return actions
    }
4.2修改側(cè)滑按鈕 UITableView 實現(xiàn)willBeginEditingRowAt代理方法
//  適配iOS 11.0之后 修改側(cè)滑刪除、分享按鈕
    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        if #available(iOS 11.0, *) {
            for subView in tableView.subviews {
                if NSStringFromClass(subView.classForCoder) == "UISwipeActionPullView" {
                    if let deleteBtn: UIButton = subView.subviews.last as? UIButton  {
                        changeAction(sourceBtn: deleteBtn, title: "取消收藏", imageStr: "icon_collecton_delete")
                    }
                    if let shareBtn: UIButton = subView.subviews.first as? UIButton  {
                        changeAction(sourceBtn: shareBtn, title: "分享", imageStr: "icon_collecton_share")
                    }
                }  else if NSStringFromClass(subView.classForCoder) == "_UITableViewCellSwipeContainerView" {
                    // iOS13.0之后
                    for sub in subView.subviews {
                        if NSStringFromClass(sub.classForCoder) == "UISwipeActionPullView" {
                            if let deleteBtn: UIButton = sub.subviews.last as? UIButton  {
                                changeAction(sourceBtn: deleteBtn, title: "取消收藏", imageStr: "icon_collecton_delete")
                            }
                            if let shareBtn: UIButton = sub.subviews.first as? UIButton  {
                                changeAction(sourceBtn: shareBtn, title: "分享", imageStr: "icon_collecton_share")
                            }
                        }
                    }
                }
            }
        }
    }

    func changeAction(sourceBtn: UIButton, title: String?, imageStr: String?) {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: sourceBtn.frame.width, height: sourceBtn.frame.height)
        btn.backgroundColor = sourceBtn.backgroundColor
        btn.setImage(UIImage(named: imageStr ?? ""), for: .normal)
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
       btn.titleLabel?.font = MSFFont.fontValue(size: 14, weight: .regular)
        if #available(iOS 13.0, *) {
            btn.titleLabel?.font = MSFFont.fontValue(size: 13, weight: .regular)
        } else {
            btn.contentHorizontalAlignment = .left
        }
      // 修改button的圖文上下顯示
        btn.updateBtnEdgeInsets(style: .Top, space: 1)
        btn.isUserInteractionEnabled = false
        sourceBtn.backgroundColor = .clear
        sourceBtn.addSubview(btn)
    }
五惨寿、修改button的圖文上下排列

enum MSFBtnEdgeInsetsStyle {
    case Top                //image在上邦泄,label在下
    case Left               //image在左, label在右
    case Bottom             //image在下裂垦,label在上
    case Right              //image在右顺囊, label在左
}

extension UIButton {
    //更新btn的圖文位置
   func updateBtnEdgeInsets(style: MSFBtnEdgeInsetsStyle, space: CGFloat) {
        let imageWidth = self.imageView?.frame.width ?? 0
        let imageHeight = self.imageView?.frame.height ?? 0
        
        let labelWidth = min(self.titleLabel?.intrinsicContentSize.width ?? 0, self.frame.width - imageWidth)
        let labelHeight = self.titleLabel?.intrinsicContentSize.height ?? 0
        
        var imageInsets = UIEdgeInsets.zero
        var labelInsets = UIEdgeInsets.zero
        
        switch style {
        case .Top:
            imageInsets = UIEdgeInsets.init(top: -(labelHeight + space) / 2, left: labelWidth / 2, bottom: (labelHeight + space) / 2, right: -labelWidth / 2 )
            labelInsets = UIEdgeInsets.init(top: (imageHeight + space) / 2, left: -imageWidth / 2, bottom: -(imageHeight + space) / 2, right: imageWidth / 2)
            break
        case .Left:
            imageInsets = UIEdgeInsets(top: 0, left: -space, bottom: 0, right: space)
            labelInsets = UIEdgeInsets(top: 0, left: space, bottom: 0, right: -space)
            break
        case .Bottom:
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -(labelHeight + space / 2), right: -labelWidth)
            labelInsets = UIEdgeInsets(top: -(imageHeight + space / 2), left: -imageWidth, bottom: 0, right: 0)
            break
        case .Right:
            imageInsets = UIEdgeInsets(top: 0, left: labelWidth + space, bottom: 0, right: -(labelWidth + space))
            labelInsets = UIEdgeInsets(top: 0, left: -(imageWidth + space), bottom: 0, right: imageWidth + space)
            break
        }
        
        self.imageEdgeInsets = imageInsets
        self.titleEdgeInsets = labelInsets
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蕉拢,隨后出現(xiàn)的幾起案子特碳,更是在濱河造成了極大的恐慌,老刑警劉巖晕换,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件午乓,死亡現(xiàn)場離奇詭異,居然都是意外死亡闸准,警方通過查閱死者的電腦和手機益愈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夷家,“玉大人蒸其,你說我怎么就攤上這事】饪欤” “怎么了摸袁?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長缺谴。 經(jīng)常有香客問我但惶,道長耳鸯,這世上最難降的妖魔是什么湿蛔? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任膀曾,我火速辦了婚禮,結(jié)果婚禮上阳啥,老公的妹妹穿的比我還像新娘添谊。我一直安慰自己,他們只是感情好察迟,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布斩狱。 她就那樣靜靜地躺著,像睡著了一般扎瓶。 火紅的嫁衣襯著肌膚如雪所踊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天概荷,我揣著相機與錄音秕岛,去河邊找鬼。 笑死误证,一個胖子當著我的面吹牛继薛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播愈捅,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遏考,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蓝谨?” 一聲冷哼從身側(cè)響起灌具,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎譬巫,沒想到半個月后稽亏,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡缕题,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年截歉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烟零。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瘪松,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出锨阿,到底是詐尸還是另有隱情宵睦,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布墅诡,位于F島的核電站壳嚎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜烟馅,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一说庭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧郑趁,春花似錦刊驴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至梭纹,卻和暖如春躲惰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背变抽。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工础拨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瞬沦。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓太伊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親逛钻。 傳聞我的和親對象是個殘疾皇子僚焦,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354