UITableView删除按钮iOS10适配

在开发中,经常会遇到修改tableView的删除按钮的情况;默认情况下,当系统语言是中文时,删除按钮显示【删除】,英文时,显示为【del】

iOS9及之前

删除按钮是cell的子视图,所以我们只需要在定制cell的时候,重写layout方法,遍历所有子视图找到删除按钮修改即可,如下:

- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews){
    if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
        CGRect cRect = subView.frame;
        cRect.origin.y = 11;
        cRect.size.height = self.contentView.frame.size.height - 11;
        subView.frame = cRect;

        UIView *confirmView=(UIView *)[subView.subviews firstObject];
        // 改背景颜色
        confirmView.backgroundColor = [CMBUITheme themePromotionColor];
        for(UIView *sub in confirmView.subviews){
            if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){
                UILabel *deleteLabel=(UILabel *)sub;
                // 改删除按钮的字体
                deleteLabel.font = [UIFont boldSystemFontOfSize:12.0];
                // 改删除按钮的文字
                deleteLabel.text = @"删除";
                deleteLabel.textColor = [CMBUITheme themeSecondaryColor];
            }
        }
        break;
    }
}
}

iOS10

但是iOS10中,删除按钮不再属于cell,而是直属于tableview,和cell是并列的关系;所以当我们采用上面的方法是无法找到的,但是可以采用同样的思路,遍历tableView的子视图即可;工作中一个示例:

- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
        subView.backgroundColor = [CMBUITheme themePageBackgroundColor];
        for (UIView *view in subView.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)view;
                CGRect cRect = btn.frame;
                cRect.origin.y = 11;
                cRect.size.height = subView.frame.size.height - 11;
                btn.frame = cRect;
                // 改删除按钮的字体
                btn.titleLabel.font = [UIFont boldSystemFontOfSize:12.0];
                // 改删除按钮的文字
                [btn setTitle:@"删除" forState:UIControlStateNormal];
                [btn setTitleColor:[CMBUITheme themeSecondaryColor] forState:UIControlStateNormal];
            }
        }
    }
}
}

自定义一个tableview继承于UITableview,之后使用自定义的视图即可;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值