4

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?

Ron
  • 389
  • 1
  • 4
  • 16
Lasang
  • 1,369
  • 6
  • 24
  • 44

9 Answers9

4

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;

         }
Suhail kalathil
  • 2,673
  • 1
  • 13
  • 12
  • in ur customCell class's layOutSubviews method – Suhail kalathil Jul 01 '14 at 05:02
  • trying it,,,,but adding at ayOutSubviews method will load each time i think – Lasang Jul 01 '14 at 05:09
  • For each cell it will getting called. i think u want delete button in every cell.r8? – Suhail kalathil Jul 01 '14 at 05:12
  • yes, i want it to every cell,,,should i make it cell.subviews or what? subview is giving error when i try to add this code inside cell for row at index path. Sorry for trouble, I am new ios developer. – Lasang Jul 01 '14 at 05:16
  • Not der man.Create a class for your cell as a subclass of UITableViewCell. Named that as MyCell, in MyCell.m class's layoutSubviews method add the above code – Suhail kalathil Jul 01 '14 at 05:20
  • Worked or not.let me know. – Suhail kalathil Jul 01 '14 at 05:48
  • I just tried if ([NSStringFromClass([cell.subviews class])isEqualToString:@"UITableViewCellDeleteConfirmationControl"]){ NSLog(@"subview found"); } but, its not going inside the loop – Lasang Jul 01 '14 at 06:14
  • see this answer too. http://stackoverflow.com/questions/2104403/iphone-uitableview-delete-button – Suhail kalathil Jul 01 '14 at 06:22
  • For iOS 10+ view hierarchy for TableViewCell swipe button has been changed, for solution see this answer: https://stackoverflow.com/questions/46338159/ios-10-uitableviewcells-delete-button-custom-height#46365375 – Muhammad Umair Oct 25 '17 at 05:12
4

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

}

Darshan Karekar
  • 681
  • 3
  • 16
Adnan Majeed
  • 141
  • 12
  • But above code works upto ios 10 but ios 11 we can,t able to see UITableViewCellDeleteConfirmationView can you give me any idea – Kamalkumar.E Oct 09 '17 at 12:01
  • 1
    @Kamalkumar.E for iOS 10+ view hierarchy has been changed, see this answer: https://stackoverflow.com/questions/46338159/ios-10-uitableviewcells-delete-button-custom-height#46365375 – Muhammad Umair Oct 25 '17 at 05:09
4

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
      }
    }
  }
tuvok
  • 185
  • 2
  • 10
  • It works for the first swipe on the same item, dismissing and swiping again is changing button size to original full height – Rick Robin Oct 26 '21 at 01:55
3

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;
    }
}
Chirag Lukhi
  • 1,528
  • 17
  • 41
2

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

        }
    }
1

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
    }
}
Marah
  • 505
  • 5
  • 10
0

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];
        }
    }
}
prema janoti
  • 159
  • 1
  • 4
0

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

WorieN
  • 1,276
  • 1
  • 11
  • 30
-1

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
        }
    }
}
Kev1n B
  • 1
  • 1
  • 1
    But above code works upto ios 10 but ios 11 we can,t able to see UITableViewCellDeleteConfirmationView can you give me any idea – Kamalkumar.E Oct 09 '17 at 12:01