iOS11下自定义左滑删除的处理.md

iOS11下自定义左滑删除的处理.md_第1张图片
示例图.jpg

在iOS11之前,如果需要自定义左滑删除的显示样式,可以通过遍历cell的子视图,找到左滑显示出来的视图,直接更改该视图即可

重写自定义CelllayoutSubviews方法

// TableViewCell.m

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            subView.backgroundColor = [UIColor colorWithHEXString:@"#F2F2F4"];
            
            for (UIView *btnView in subView.subviews) {
                if ([btnView isKindOfClass:NSClassFromString(@"_UITableViewCellActionButton")]) {
                    UIButton *btn = (UIButton *)btnView;
                    btn.bounds = CGRectMake(0, 0, 50, 50);
                    btn.backgroundColor = [UIColor whiteColor];
                    btn.layer.cornerRadius = 25;
                    [btn setBackgroundImage:[UIImage imageNamed:@"collection_item_delete"] forState:UIControlStateNormal];
                    
                    // 移除标题
                    for (UIView *view in btn.subviews) {
                        if ([view isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                            [view removeFromSuperview];
                            break;
                        }
                    }
                }
            }
        }
    }
}

但是在iOS11之后左滑删除的视图层级发生了变化。该视图不再属于Cell,而是属于tableView。
所以再更改该样式时需要在tableView上面来进行更改。

视图层级改变为 UITableView -> UISwipeActionPullView

所以更改该界面的方法如下


// ViewController.m

- (void)configSwipeButton
{
    // iOS 11层级: UITableView -> UISwipeActionPullView
    for (UIView *subview in self.tableView.subviews)
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
        {
            subview.backgroundColor = [UIColor colorWithHEXString:@"F2F2F4"];
            UIButton *deleteBtn = subview.subviews.firstObject;
            [deleteBtn setImage:[UIImage imageNamed:@"collection_item_delete"] forState:UIControlStateNormal];
            deleteBtn.backgroundColor = [UIColor colorWithHEXString:@"F2F2F4"];
            [deleteBtn setTitle:@"" forState:UIControlStateNormal];
            
            CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, [[UIColor color5] CGColor]);
            CGContextFillRect(context, rect);
            UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            [deleteBtn setBackgroundImage:theImage forState:UIControlStateHighlighted];
            [deleteBtn setBackgroundImage:theImage forState:UIControlStateNormal];
        }
    }
}
注意点1

iOS11之前,对视图的调整是在自定义的Cell中的调整
而iOS11之后是在tableView所在的ViewController中来调整

注意点2

上述方法中通过代码画出来一张和背景色相同的图片,作为按钮的高亮状态的背景图。

如果不进行这步操作,左滑出来之后样式是正常的,但是当点击时按钮背景色会变为默认的红色。所以需要设置其高亮状态的背景图片。

注意点3

UISwipeActionPullView该视图只有在tableView处于编辑模式的时候才会出现。所以上面这个方法的调用时机应该是在tableView处于左滑状态时,即编辑模式下。所以需要在tableView的进入编辑模式的代理方法中进行如下监听处理

// ViewController.m

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    self.editingIndexPath = indexPath;
    [self.view setNeedsLayout];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    self.editingIndexPath = nil;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.editingIndexPath && ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)) {
        [self configSwipeButton];
    }
}

定义一个属性用于判断当前是否处理编辑模式。用BOOL即可

当tableView进入编辑模式时,将该属性设置为YES,同时调用setNeedsLayout来进行界面重绘。

界面重新绘制会进入到viewDidLayoutSubviews方法中,在该方法内检测是否处于编辑状态并且系统版本高于iOS11。

符合判断后 重新调整左滑后的样式

你可能感兴趣的:(iOS11下自定义左滑删除的处理.md)