A, requirements,

First of all, let’s talk about our requirements. In a tableView, when you delete a cell by sliding left, you need to show the style as shown in the following figure, light gray background color, orange text.

1. Modify the text of delete button

Changing the delete button text is as simple as implementing the following method:

// Modify the edit button text
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Delete";
}
Copy the code

2, modify the delete button background color and text color

First of all, I according to the conventional processing in editActionsForRowAtIndexPath method:

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    // delete action
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    }];
    deleteAction.backgroundColor = [UIColor colorWithHexString:Color_F7F7F7];
    return @[deleteAction];
}
Copy the code

But found only through deleteAction. BackgroundColor change background color, but can’t change the font color. And the system to provide several UITableViewRowActionStyle also does not meet my requirements:

typedef NS_ENUM(NSInteger.UITableViewRowActionStyle) {
    UITableViewRowActionStyleDefault = 0.UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault.UITableViewRowActionStyleNormal
}
Copy the code

The appearance of the default UITableViewRowActionStyleDefault:

The appearance of UITableViewRowActionStyleNormal:

Second, solutions

I found the solution from the Internet, the latest iOS12 is my own experiment. The solution will also change as the iOS system is upgraded, because the structure of the system controls may change, causing the view to be traversed without finding it.

1, the system version < iOS 11 processing mode

IOS11 processing way is to traverse the Cell before subViews find UITableViewCellDeleteConfirmationView child views, just in the Cell. M add layoutSubviews code files:

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subView in self.subviews) {
        if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
            
            UIButton *bgView = (UIButton *)[subView.subviews firstObject];
            bgView.backgroundColor = [UIColor colorWithHexString:Color_F0F0F0];
            [bgView setTitleColor:[UIColor colorWithHexString:Color_MainColor] forState:UIControlStateNormal]; }}}Copy the code

2. IOS 11 <= System version < iOS 13 processing mode

This system version needs to add layoutSubviews to the tableView. I wrote a tableView parent class and added layoutSubviews to the.m of the parent class. I also highlighted a cellHeightRef in the.h of the tableView to delete the height of the Button.

  • Is simply in the tableView subviews traversal UISwipeActionPullView, traversal in UISwipeActionPullView UISwipeActionStandardButton. Then change the style of button.
- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
            // Change the background color
            subview.backgroundColor =  [UIColor clearColor];
            // Modify button - color
            UIButton *swipeActionStandardBtn = subview.subviews[0];
            if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
                    CGFloat swipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;
                    swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 0, swipeActionStandardBtnW, self.cellHeightRef - 10);

                    [swipeActionStandardBtn setTitleColor:[UIColor colorWithHexString:Color_MainColor] forState:UIControlStateNormal];
                    [swipeActionStandardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                    [swipeActionStandardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; }}}}Copy the code

3, system version == iOS 13 processing mode (greater than 13 is unknown, I will update when a new one comes out)

IOS 13 and the iOS 13 above method is almost the same as before, just the inside of the tableView father-son relationship has changed, view UISwipeActionStandardButton location changed. The original change the subView to subView. Subviews. FirstObject, can obtain the UISwipeActionStandardButton.

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subview in self.subviews) {
        if ([subview.subviews.firstObject isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
            // Change the background color
            subview.subviews.firstObject.backgroundColor =  [UIColor clearColor];
            // Modify button - color
            UIButton *swipeActionStandardBtn = subview.subviews.firstObject.subviews[0];
            if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
                    CGFloat swipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;
                    swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 0, swipeActionStandardBtnW, self.cellHeightRef - 10);

                    [swipeActionStandardBtn setTitleColor:[UIColor colorWithHexString:Color_MainColor] forState:UIControlStateNormal];
                    [swipeActionStandardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                    [swipeActionStandardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; }}}}Copy the code