1.自定義左滑樣式
單個左滑按鈕的情況下可以使用圖片轉(zhuǎn)color的方式設(shè)置UITableViewRowAction
的背景色:
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//刪除動作
}];
deleteAction.backgroundColor = [UIColor colorWithPatternImage:[self setBackImageView]];
return @[deleteAction];
}
//自定義視圖
-(UIImage *)setBackImageView{
UIButton *btn = [UIButton buttonWithType:0];
btn.frame = CGRectMake(0, 0, 100, 124);
btn.backgroundColor = kMainColor;
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(30, 46, 16, 18)];
imageV.image = kIMAGE_Name(@"shop_car_deleteIcon1");
[btn addSubview:imageV];
UILabel *titleLabel = [btn createLabelFrame:CGRectMake(20, 70, 36, 20) textColor:kWhiteColor font:kFont(13)];
titleLabel.text = @"刪除";
titleLabel.textAlignment = NSTextAlignmentCenter;
return [UIView convertViewToImage:btn];
}
view的擴(kuò)展方法
@implementation UIView (Extention)
//View轉(zhuǎn)Image
//使用該方法不會模糊姜盈,根據(jù)屏幕密度計算
+ (UIImage *)convertViewToImage:(UIView *)view {
UIImage *imageRet = [[UIImage alloc]init];
//UIGraphicsBeginImageContextWithOptions(區(qū)域大小, 是否是非透明的, 屏幕密度);
UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
imageRet = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageRet;
}
@end
這時候可能會出現(xiàn)UI上的瑕疵,如圖:
圖上的淺灰色是由于視圖
UISwipeActionPullView
的默認(rèn)背景色馆蠕,這時候就需要找到對應(yīng)的視圖進(jìn)行更改
2.查找UISwipeActionPullView
step1.在tableview的代理方法willBeginEditingRowAtIndexPath
里調(diào)用
這一步必須進(jìn)行,否則當(dāng)前控制器的viewWillLayoutSubviews
不會執(zhí)行
[self.view setNeedsLayout];
step2.重寫viewWillLayoutSubviews方法蔑滓,查找到UISwipeActionPullView
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
UIView *targetView;
for (UIView *t_subView in self.shopcartTableView.subviews) {
if (@available(iOS 13.0, *)) {
if ([NSStringFromClass([t_subView class]) isEqualToString:@"_UITableViewCellSwipeContainerView"]) {
for (UIView *t2_subView in t_subView.subviews) {
if ([NSStringFromClass([t2_subView class]) isEqualToString:@"UISwipeActionPullView"]){
targetView = t2_subView;
}
}
}
} else {
if ([NSStringFromClass([t_subView class]) isEqualToString:@"UISwipeActionPullView"]){
targetView = t_subView;
}
}
targetView.backgroundColor = kBackgroundColor;
NSLog(@"====%@",targetView);
}
}