'Set height of delete button that appears on swipe in UITableViewCell

enter image description hereI have UITableViewCell as shown in figure below.

The cell occupy the height occupied by delete. The cell height is set so as to keep spacing between two cell.

Now, when i swipe and delete button appears (red in color), it occupies cell height as given in picture above. I simply want to set its height to height of white part only or say the height of gray button. Can anyone help me on how to set the height of delete button that appears after swipe in UITableViewCell?



Solution 1:[1]

The best way to solve this was overriding

-(void)layoutSubviews in YourCustomCell:UITableViewCell    

then

if ([NSStringFromClass([subview class])isEqualToString:@"UITableViewCellDeleteConfirmationControl"]){
            UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
                                CGRect buttonFrame = deleteButtonView.frame;
                                buttonFrame.origin.x = Xvalue;
                                buttonFrame.origin.y = Yvalue;
                                buttonFrame.size.width = Width;
                                buttonFrame.size.height = Height;
                                deleteButtonView.frame = buttonFrame;

         }

Solution 2:[2]

Use this code in your custom Cell class

 -(void) layoutSubviews
{
    NSMutableArray *subviews = [self.subviews mutableCopy];
    UIView *subV = subviews[0];
    if ([NSStringFromClass([subV class])isEqualToString:@"UITableViewCellDeleteConfirmationView"]){
      [subviews removeObjectAtIndex:0];
      CGRect f = subV.frame;
      f.size.height = 106; // Here you set height of Delete button
      subV.frame = f;
     }

}

Solution 3:[3]

Swift 5, works for iOS12, iOS13 and iOS14

  func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    // for iOS13, iOS14
    if let swipeContainerView = tableView.subviews.first(where: { String(describing: type(of: $0)) == "_UITableViewCellSwipeContainerView" }) {
      if let swipeActionPullView = swipeContainerView.subviews.first, String(describing: type(of: swipeActionPullView)) == "UISwipeActionPullView" {
        swipeActionPullView.frame.size.height -= 10
      }
    }

    // for iOS12
    tableView.subviews.forEach { subview in
      if String(describing: type(of: subview)) == "UISwipeActionPullView" {
        subview.frame.size.height -= 10
      }
    }
  }

Solution 4:[4]

Add this method to your customCell.m file.

-(void) layoutSubviews
{
    NSMutableArray *subviews = [self.subviews mutableCopy];
    UIView *subview = subviews[0];

    if ([NSStringFromClass([subview class])isEqualToString:@"UITableViewCellDeleteConfirmationView"]){
        UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
        CGRect buttonFrame = deleteButtonView.frame;
        buttonFrame.origin.x = deleteButtonView.frame.origin.x;
        buttonFrame.origin.y = deleteButtonView.frame.origin.y;
        buttonFrame.size.width = deleteButtonView.frame.size.width;
        buttonFrame.size.height = 46;
        deleteButtonView.frame = buttonFrame;
        subview.frame=CGRectMake(subview.frame.origin.x, subview.frame.origin.y, subview.frame.size.width, 46);
        deleteButtonView.clipsToBounds=YES;
        subview.clipsToBounds=YES;
    }
}

Solution 5:[5]

For IOS 13 , the Position has been yet again change , not inside table view it is once again in _UITableViewCellSwipeContainerView . Thus you should iterate through that as well.Take a look below

([NSStringFromClass([subview class]) 
 isEqualToString:@"_UITableViewCellSwipeContainerView"]){

        for (UIView *deleteButtonSubview in subview.subviews){

            if ([NSStringFromClass([deleteButtonSubview class])
                 isEqualToString:@"UISwipeActionPullView"]) {



                if ([NSStringFromClass([deleteButtonSubview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {

                   //do what you want
                }
            }

        }
    }

Solution 6:[6]

Write below code in your custom cell hope it will work for you-

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];
    if(state == UITableViewCellStateShowingDeleteConfirmationMask)
     {
        [self performSelector:@selector(resetDeleteButtonSize) withObject:nil afterDelay:0];
     }
 }



- (void)resetDeleteButtonSize
{
    NSMutableArray *subviews = [self.subviews mutableCopy];
    while (subviews.count > 0)
    {
        UIView *subV = subviews[0];
        [subviews removeObjectAtIndex:0];
        if ([NSStringFromClass([subV class])isEqualToString:@"UITableViewCellDeleteConfirmationButton"])
        {
            CGRect f = subV.frame;
            f.size.height = 74;
            subV.frame = f;
            break;
        }
        else
        {
            [subviews addObjectsFromArray:subV.subviews];
        }
    }
}

Solution 7:[7]

To see how it's work in IOS 11 please copy this Swift 4 code snippet in your UITableViewController:

 override func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    self.tableView.subviews.forEach { subview in
        print("YourTableViewController: \(String(describing: type(of: subview)))")
        if (String(describing: type(of: subview)) == "UISwipeActionPullView") {
            if (String(describing: type(of: subview.subviews[0])) == "UISwipeActionStandardButton") {
                var deleteBtnFrame = subview.subviews[0].frame
                deleteBtnFrame.origin.y = 12
                deleteBtnFrame.size.height = 155


                // Subview in this case is the whole edit View
                subview.frame.origin.y =  subview.frame.origin.y + 12
                subview.frame.size.height = 155
                subview.subviews[0].frame = deleteBtnFrame
                subview.backgroundColor = UIColor.yellow
            }
        }
    }
}

This code working for IOS 11 and higher

Solution 8:[8]

Swift 5 - iOS 14 Change the way you are handling cell height to add spacing using the following:

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 10
        super.frame = frame
    }
}

Solution 9:[9]

SWIFT

override func layoutSubviews() {
    super.layoutSubviews()
    for subview in self.subviews {
        if String(describing: type(of: subview.self)) == "UITableViewCellDeleteConfirmationView" {
            let deleteButton = subview
            let deleteButtonFrame = deleteButton.frame
            let newFrame = CGRect(x: deleteButtonFrame.minX, 
                                  y: deleteButtonFrame.minY,
                                  width: deleteButtonFrame.width,
                                  height: yourHeight)
            deleteButton.frame = newFrame
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Darshan Karekar
Solution 3
Solution 4 iChirag
Solution 5 Anjula Serasinghe
Solution 6 prema janoti
Solution 7 WorieN
Solution 8 Marah
Solution 9