iOS左滑插入多个按钮,按钮自定义颜色、图片、文字字体等
2017-08-16 17:42
537 查看
iOS8.0之后UITableView新添加了,左滑可以添加多个按钮的方法
自定义左滑按钮,UITableViewRowAction,在你自定义的cell或者UITableViewCell中,重写-(void)layoutSubviews方法
- (void)layoutSubviews{
for (UIView *subView in self.subviews) {
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
NSArray *array = @[@"icon_mp_dete",@"icon_mp_fz",@"icon_mp_tele",@"icon_mp_message"];
for(int i=0; i<4; i++){
UIView *btnView = subView.subviews[i];
btnView.backgroundColor = HEX_COLOR(@"afb6c1");
for (UIView *btn in btnView.subviews) {
UIImageView *imageview = [[UIImageView alloc] init];
imageview.contentMode = UIViewContentModeScaleAspectFit;
imageview.image = kImageWithName(array[i]);
imageview.frame = CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height);
[btn addSubview:imageview];
}
}
}
}
}
效果如下:
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ __weak typeof(self) weakSelf = self; NSMutableArray *array = [NSMutableArray array]; //插入多个按钮,按钮位置排序0~4,从右向左 for(int i=0; i<4; i++){ __block NSInteger tag = i; UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakSelf tapRowAction:indexPath.row type:tag]; }]; rowAction.backgroundColor = HEX_COLOR(@"afb6c1"); [array addObject:rowAction]; } return array; } - (void)tapRowAction:(NSInteger)row type:(NSInteger)type{ if(type==0){ [[[CommonUIAlert alloc] init] showCommonAlertView:self title:@"" message:@"是否删除该名片" cancelButtonTitle:@"取消" otherButtonTitle:@"确定" cancle:^{ } confirm:^{ [self.dataArray removeObjectAtIndex:row]; [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }]; }else if(type==1){ CardGroupViewController *vc = [CommonMethod getVCFromNib:[CardGroupViewController class]]; vc.isShowGroupList = NO; [self.navigationController pushViewController:vc animated:YES]; }else if(type==2){ NSString *str = [NSString stringWithFormat:@"tel:%@",@"15261173162"]; UIWebView *callWebView = [[UIWebView alloc]init]; [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; [self.view addSubview:callWebView]; }else{ [self showMessageView:[NSArray arrayWithObjects:@"15261173162", nil] title:@""]; } }
自定义左滑按钮,UITableViewRowAction,在你自定义的cell或者UITableViewCell中,重写-(void)layoutSubviews方法
- (void)layoutSubviews{
for (UIView *subView in self.subviews) {
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
NSArray *array = @[@"icon_mp_dete",@"icon_mp_fz",@"icon_mp_tele",@"icon_mp_message"];
for(int i=0; i<4; i++){
UIView *btnView = subView.subviews[i];
btnView.backgroundColor = HEX_COLOR(@"afb6c1");
for (UIView *btn in btnView.subviews) {
UIImageView *imageview = [[UIImageView alloc] init];
imageview.contentMode = UIViewContentModeScaleAspectFit;
imageview.image = kImageWithName(array[i]);
imageview.frame = CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height);
[btn addSubview:imageview];
}
}
}
}
}
效果如下:
相关文章推荐
- iOS开发关于UISearchBar自定义取消按钮的颜色,字体,大小
- iOS自定义Button如何在按钮中把文字放左边,图片放右边
- IOS 初学 runtime 自定义Button实现按钮图片在上文字在下
- iOS 自定义textField 显示下划线 左边显示文字 自定义placeholder的颜色和字体
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- 网页制作常用代码(文字颜色/字体/插入图片/flash等
- 自定义iOS导航栏背景,标题和返回按钮文字颜色
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- iOS 更改tabbar字体颜色 自定义controller返回按钮
- JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能
- IOS开发中如何自定义导航栏的按钮,文字和背景图片
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- iOS心得7 按钮的图片和文字自定义位置
- ios之自定义导航栏背景,标题和返回按钮文字颜色
- IOS 开发之自定义按钮实现文字图片位置随意定制
- iOS 7+自定义导航栏背景,标题和返回按钮文字颜色
- iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮
- IOS学习 NSNavigationController 自定义:左右按钮、中间文字、背景图片
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- IOS设置导航栏全局的背景图片、导航栏title文字颜色大小、导航栏左右按钮的颜色。