自定义UITableViewRowAction按钮
我想为uitableviewrowaction按钮设置顶部和底部约束
这是我的代码
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ }]; deleteAction.backgroundColor = [UIColor redColor]; return @[deleteAction]; }
像这样我添加了删除按钮。 在tableviewCell
我添加了一个UIView
它有顶部和底部约束。 我希望删除按钮与UITableviewCell
视图相UITableviewCell
。
您可以在自定义uitableviewcell类中设置删除按钮框
喜欢这个
-(void)didTransitionToState:(UITableViewCellStateMask)state { [super didTransitionToState:state]; if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) { UIView *deleteButton = [self deleteButtonSubview:self]; if (deleteButton) { CGRect frame = deleteButton.frame; frame.origin.y = 4; frame.size.height = frame.size.height-8; /* if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { frame.size.height = 62; //vikram singh 2/1/2015 frame.size.width = 80; } else { frame.size.height = 52; //vikram singh 2/1/2015 frame.size.width = 80; } */ deleteButton.frame = frame; } } } - (UIView *)deleteButtonSubview:(UIView *)view { if ([NSStringFromClass([view class]) rangeOfString:@"Delete"].location != NSNotFound) { return view; } for (UIView *subview in view.subviews) { UIView *deleteButton = [self deleteButtonSubview:subview]; [deleteButton setBackgroundColor:[UIColor whiteColor]]; if (deleteButton) { return deleteButton; } } return nil; }
使用didTransitionToState方法:)
您可以使用自定义类来实现此function。 请点击链接
更多关于Balkaran的回答…删除按钮是一个自定义私有类,但使用String(describing:)
方法,你可以合理地确定你可以得到它。
另外,我很惊讶地发现,一旦你开始改变状态, didTransition
激活,而不是在状态完成改变时。
Swift 3中@ balkaran代码的更新版本:
override func didTransition(to state: UITableViewCellStateMask) { super.willTransition(to: state) if state == .showingDeleteConfirmationMask { let deleteButton: UIView? = subviews.first(where: { (aView) -> Bool in return String(describing: aView).contains("Delete") }) if deleteButton != nil { deleteButton?.frame.size.height = 50.0 }
super.willTransitionToState(state) if state == .ShowingDeleteConfirmationMask { let deleteButton: UIView? = subviews[0] if deleteButton != nil { var frame: CGRect? = deleteButton?.frame frame?.origin.y = 9 frame?.origin.x = 10 frame?.size.height = (frame?.size.height)! - 14 deleteButton?.frame = frame! } }