Search code examples
iosobjective-cuitableviewrowaction

Custom UITableViewRowAction button


I want to set top and bottom constraint for uitableviewrowaction button

Here's my code

- (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];
  }

Like this I've added delete button. In tableviewCell I've added one UIView it has top and bottom constraints. I want the delete button to match with my view in UITableviewCell.

enter image description here


Solution

  • you can set delete button frame in your custom uitableviewcell class

    like this

     -(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;
    }
    

    use didTransitionToState methods :)