Set height of delete button that appears on swipe in UITableViewCell

。_饼干妹妹 提交于 2019-12-03 08:57:00

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;

         }
Adnan Majeed

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

}

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

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

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

        }
    }

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

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
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!