iOS 自定義UITabView左滑樣式及滑動背景UISwipeActionPullView

1.自定義左滑樣式

單個(gè)左滑按鈕的情況下可以使用圖片轉(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ù)屏幕密度計(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

這時(shí)候可能會出現(xiàn)UI上的瑕疵,如圖:

image.png

圖上的淺灰色是由于視圖UISwipeActionPullView的默認(rèn)背景色,這時(shí)候就需要找到對應(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);
       }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容