在开发中,经常会遇到修改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,之后使用自定义的视图即可;