iOS UITableViewCell 左滑删除时,修改删除按钮背景颜色,默认是红色的

方法1:

 1 // 自定义左滑显示编辑按钮
 2 -(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     
 5     UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
 6                                                                          title:@"跟进" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
 7                                                                              NSLog(@"跟进");
 8                                                                             
 9                                                                          }];
10     
11     UITableViewRowAction *rowActionSec = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
12                                                                             title:@"快速备忘"    handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
13                                                                                 NSLog(@"快速备忘");
14                                                                                 
15                                                                             }];
16     rowActionSec.backgroundColor = [UIColor colorWithHexString:@"f38202"];
17     rowAction.backgroundColor = [UIColor colorWithHexString:@"d9d9d9"];
18     
19     NSArray *arr = @[rowAction,rowActionSec];
20     return arr;
21 }

方法2:

开发中可能会有改变这个按钮背景色的需求,如微信的通讯录左滑备注按钮就是灰色的,实现这个需求我们需要自定义cell,在自定义的cell中重写layoutSubviews这个方法,找到UITableViewCellDeleteConfirmationView修改它的背景色即可。

 1 - (void)layoutSubviews
 2 {
 3     [super layoutSubviews];
 4 
 5     for (UIView *subView in self.subviews) {
 6         NSString *subViewName =NSStringFromClass([subView class]);
 7         if ([subViewName isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
 8             subView.backgroundColor = [UIColor lightGrayColor];
 9             ((UIView *)[subView.subviews firstObject]).backgroundColor = [UIColor lightGrayColor];
10 
11         }
12     }
13 }
原文地址:https://www.cnblogs.com/codemakerhj/p/6609883.html