一哮独、需求:
需要左滑刪除按鈕需要圖文上下顯示(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
}
}