美文网首页
iOS13修改左滑按钮失效 UITableView左滑删除修改样

iOS13修改左滑按钮失效 UITableView左滑删除修改样

作者: 扛支枪 | 来源:发表于2019-12-04 16:23 被阅读0次

    首先ios不同版本对应的层级不一样,所以要不同的设置
    我的需求只是因为删除按钮的高度比我的cell高,所以只是这只了按钮的位置和大小,如果你想要加按钮还是在同样的位置添加按钮即可,可以试试哦

    ios8-10对应的设置

    在自己的cell中添加如下代码

    - (void)layoutSubviews {
        /**自定义设置iOS8-10系统下的左滑删除按钮大小*/
        for (UIView * subView in self.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
                //设置按钮frame
                CGRect cRect = subView.frame;
                cRect.origin.y = 7;
                cRect.size.height = 120;
                subView.frame = cRect;
            }
        }
    }
    
    

    ios11-13对应的设置

    此处的一个坑是ios13和ios11-12的层又有区别,就是有加了一层_UITableViewCellSwipeContainerView,所以我就又做了一层判断,本来想直接用 if (@available(iOS 13.0, *)) 判断的,但是怕ios12又作妖,所以就直接拿名字判断靠谱点

    - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
        if (@available(iOS 11.0, *)) {
            for (UIView * subview in self.tableView.subviews) {
                //ios 13 又增加一层_UITableViewCellSwipeContainerView
                if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
                    for (UIView * subView in subview.subviews) {
                        if ([self setDeleteBtn:subView]) {
                            return;
                        }
                    }
                }else{
                    if ([self setDeleteBtn:subview]) {
                        return;
                    }
                }
            }
        }
    }
    
    //设置删除按钮位置
    - (BOOL)setDeleteBtn:(UIView *)subView{
        if (subView.tag == 10086) {//这个作用看文末解释,至今所有写设置删除按钮的文章都没说到这个问题
            return NO;
        }
        if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
            //设置按钮frame
            for (UIView * sonView in subView.subviews) {
                if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    CGRect cRect = sonView.frame;
                    cRect.origin.y = 7;
                    cRect.size.height = 120;
                    sonView.frame = cRect;
                    subView.tag = 10086;
                    return YES;
                }
            }
            
        }
        return NO;
    }
    

    划重点:那个判断tag=10086的作用,如果不加判断,当左滑出来一个删除按钮,然后让此按钮再滑回去,再重复这样做的话没有任何问题;但是,当你滑开cell2中删除按钮再滑开另外一个cell1(cell1在cell2上边,即cell2的indexpath的row是2,cell1的indexpath的row是1)中的按钮,就会出现这个新滑开的按钮样式没有改变,很奇怪吧,其实这个时候tableview上会有两个删除按钮,你的for循环很大可能遍历到你之前已经改过样式的按钮,所以就不会再遍历新的按钮了。所以加个判断,当修改过的按钮被遍历到就不管继续遍历即可。

    相关文章

      网友评论

          本文标题:iOS13修改左滑按钮失效 UITableView左滑删除修改样

          本文链接:https://www.haomeiwen.com/subject/hxelgctx.html