美文网首页
IOS获取cell上的左滑删除按钮 并设置样式

IOS获取cell上的左滑删除按钮 并设置样式

作者: 真爱要有你才完美 | 来源:发表于2018-06-27 13:36 被阅读160次

自从IOS11出现以后tableviewcell 还有很多的方法都出现了不同的改变

比如我前段时间 为了做

自定义的cell的高度和这个设备的白色快的view同一个高度 ,在IOS 10 以前 我们调用

其实原理大家应该知道就是因为层级关系

IOS11之前的方法 IOS11之前全部都是跟cell是从属关系

iOS11之后,自定义删除按钮 我们会发现 这个层级关系就变了  他们在tableview上是同一级别的关系因此我们以前使用的改变 自定义按钮的样式的方法就不能用了

iOS11之后, 这个方法不适用,看下图的对比

UITableViewCellDeleteConfirmationView这个系统隐藏的视图的出现时机,是出现在侧滑的cell中,作为其subView存在.所以修改按钮样式的代码,是写在自定义的cell.m中.

iOS11之后

所以,在iOS11之后,自定义的代码可以写在UITableview中,

创建一个继承UITableview的类,重写它的layoutSubviews方法,根据图中的结构,遍历出UISwipeActionPullView进行修改

iOS11以后适配代码

就是在自己自定义的tableview中重写layoutsubviews方法

代码如下:

- (void)layoutSubviews{

    [super layoutSubviews];

    //iOS11版本以上,自定义删除按钮高度方法:

    if (IOS_VERSION_11) {

        for(UIView * subview in self.subviews)

        {

            if([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])

            {

                UIView*swipeActionPullView = subview;

                //1.0修改背景颜色

                swipeActionPullView.backgroundColor=  [UIColor clearColor];

                //1.1修改背景圆角

                swipeActionPullView.layer.cornerRadius=5.f;

                swipeActionPullView.layer.masksToBounds=YES;

                //2.0修改按钮-颜色

                UIButton*swipeActionStandardBtn = subview.subviews[0];

                if([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {

                    CGFloatswipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;

                    CGFloatswipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;

                    swipeActionStandardBtn.frame=CGRectMake(swipeActionStandardBtnOX,10, swipeActionStandardBtnW,82-10);

                    //2.1修改按钮背景色

                    swipeActionStandardBtn.backgroundColor=  [UIColorcolorWithRed:255/255.fgreen:173/255.fblue:69/255.falpha:1.0f];

                    //2.2修改按钮背景圆角

                    swipeActionStandardBtn.layer.cornerRadius=5.f;

                    swipeActionStandardBtn.layer.masksToBounds=YES;

                }

            }

        }

    }

}

相关文章

网友评论

      本文标题:IOS获取cell上的左滑删除按钮 并设置样式

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