原理:遍历view层级,找到删除按钮图层,然后就可以为所欲为了😏。
修改代码,封装在UITableView类别里,方便调用。
@interface UITableView (DeleteButtonFrame)
- (void)setupSlideDeleteBtnOriginY:(CGFloat)originY height:(CGFloat)height editingIndexPath:(NSIndexPath*)editingIndexPath;
@end
#import "UITableView+DeleteButtonFrame.h"
@implementation UITableView (DeleteButtonFrame)
- (void)setupSlideDeleteBtnOriginY:(CGFloat)originY height:(CGFloat)height editingIndexPath:(NSIndexPath*)editingIndexPath{
dispatch_async(dispatch_get_main_queue(), ^{
[self setupCellDeleteBtnOriginY:originY height:height editingIndexPath:editingIndexPath];
});
}
#pragma mark - Private
- (void)setupCellDeleteBtnOriginY:(CGFloat)originY height:(CGFloat)height editingIndexPath:(NSIndexPath*)editingIndexPath{
// 判断系统是否是 iOS13 及以上版本
if (@available(iOS 13.0, *)) {
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
// 修改删除按钮
UIView *remarkContentView = subView.subviews.firstObject;
[self setupRowActionView:remarkContentView oy:originY h:height];
}
}
return;
}
// 判断系统是否是 iOS11 及以上版本
if (@available(iOS 11.0, *)) {
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
// 修改删除按钮
UIView *remarkContentView = subView;
[self setupRowActionView:remarkContentView oy:originY h:height];
}
}
return;
}
// iOS11 以下的版本
UITableViewCell *cell = [self cellForRowAtIndexPath:editingIndexPath];
for (UIView *subView in cell.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
// 修改尺寸
UIView *remarkContentView = subView;
[self setupRowActionView:remarkContentView oy:originY h:height];
}
}
}
- (void)setupRowActionView:(UIView*)actionView oy:(CGFloat)oy h:(CGFloat)h {
CGRect frame = actionView.frame;
frame.origin.y = oy;
frame.size.height = h;
actionView.frame = frame;
}
@end
如何使用?这里是关键。
在两个地方调用:
1.当cell将要开始编辑的delegate方法中
2.指定cell的可编辑类型时
注意:原则上,只有1调用就可以了,但是会有一个小bug,但是我不说😄,所以两处都要调用。
调用代码
// MARK: - 左滑删除
///iOS11以下,使用此方法
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
///此时调用是为了防止:当结束编辑时,删除按钮还未完全消失时,
///再次左滑,修改会失效,所以再次修改下
self.setupDeleteBtnFrame(indexPath: indexPath)
let deleteAction = UITableViewRowAction.init(style: .default, title: "删除") { (aciton, indexPath) in
print("删除完成")
///点击删除后,结束编辑
tableView.setEditing(false, animated: true)
}
return [deleteAction]
}
///iOS11及以上使用此方法
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
///此时调用是为了防止:当结束编辑时,删除按钮还未完全消失时,
///再次左滑,修改会失效,所以再次修改下
self.setupDeleteBtnFrame(indexPath: indexPath)
func handleDelete(action: UIContextualAction, view: UIView, endEditing:@escaping (Bool)->Void){
print("删除成功")
///点击删除后,结束编辑
endEditing(true)
}
let handle : UIContextualAction.Handler = handleDelete(action:view:endEditing:)
let deleteAction = UIContextualAction.init(style: .destructive, title: "删除", handler: handle)
let conf = UISwipeActionsConfiguration.init(actions: [deleteAction])
return conf
}
///修改删除按钮大小
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
///当即将开始编辑,修改删除按钮的大小
self.setupDeleteBtnFrame(indexPath: indexPath)
}
func setupDeleteBtnFrame(indexPath: IndexPath){
self.tableView.setupSlideDeleteBtnOriginY(10, height: 90, editing: indexPath)
}
上Demo, 因为最近github经常那个,所以放到gitee上了。
----------------------- 发现了另一个调用方法🙃----------------------
使用系统的自带的删除类型。在以下两处调用,就不用自定义action了。
// MARK: - 左滑删除
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
print("开始删除:\(Date())");
self.tableView.setupSlideDeleteBtnOriginY(12.5, height: 30.0, editing: indexPath)
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
self.tableView.setupSlideDeleteBtnOriginY(12.5, height: 30.0, editing: indexPath)
return .delete
}
网友评论