美文网首页iOS bug 收录
更改UITableViewCell左滑按钮字体颜色

更改UITableViewCell左滑按钮字体颜色

作者: 墨殇丿雨 | 来源:发表于2018-05-05 14:05 被阅读0次

最近在项目中遇到一个需求,更改cell左滑时出现的删除按钮的字体颜色,当时想到的第一个方法就是用UITableViewRowAction设置字体的颜色,然而发现这个里面并没有可以直接设置的相关属性。
后来在网上搜索相关文章,得到了一些线索。最后完成了,现在来说一下踩的坑。

之前在网上找到了一个解决方法,用CV大法把别人的劳动成功拿过来,发现并不能行。
后来仔细查看了层级图才发现区别,原来在iOS11上面cell左滑出现的删除按钮不是加载在cell上面的,是直接加载tableview上面的。
层级图TableView--UISwipeActionPullView--UISwipeActionStandardButton--UIButtonLabel
这个UIButtonLabel才是我们最终要修改的东西。

iOS11系统下的层级图 屏幕快照 2018-05-05 12.35.44.png

其他系统下

TableViewCell-- UITableViewCellDeleteConfirmationView--UITableViewCellActionButton--UIButtonLabel 屏幕快照 2018-05-05 13.35.11.png
上代码
- (void)willTransitionToState:(UITableViewCellStateMask)state{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (@available(iOS 11.0,*)) {
            for (UIView * lview in self.superview.subviews.lastObject.subviews) {
                    for (UIView * subcaceView in lview.subviews) {
                        if ([subcaceView isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                            [subcaceView setValue:@"删除" forKey:@"text"];
                            [subcaceView setValue:UIColor.blackColor forKey:@"textColor"];
                        }
                    }
                }
        }else{
            for (UIView *subView in self.subviews) {
                if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
                    for (UIView * subcaceView in subView.subviews) {
                        for (UIView * labelButtonView in subcaceView.subviews) {
                            if ([labelButtonView isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                                [labelButtonView setValue:@"删除" forKey:@"text"];
                                [labelButtonView setValue:UIColor.blackColor forKey:@"textColor"];
                            }
                        }
                    }
                }
            }
        }
    });
}

然而发现在iOS11的系统下,滑动一个Cell进入编辑状态之后,在滑动下一个cell进入编辑状态,发现并不是每一次cell改变编辑状态会调用这个方法,这就会造成cell的文字颜色并没有被改变。
最后想到tableview代理方法里面去实现改变字体颜色的方法

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ...
    });
}

参考文章 https://blog.csdn.net/zhz459880251/article/details/49839105

相关文章

网友评论

    本文标题:更改UITableViewCell左滑按钮字体颜色

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