Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10: UITableViewCell's delete button custom height

Using custom UITableViewCell, I'm trying to change the height of tableViewCell's delete button. I've tried all the solutions available here on SO.

Everyone has mentioned that in customTableViewCell class we need to override layoutSubviews method and iterate over self.subViews to find a subView which should be equal to UITableViewCellDeleteConfirmationView or in other iOS versions it is UITableViewCellDeleteConfirmationControl so I have used following code:

- (void)layoutSubviews
{
    [super layoutSubviews];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0f];

    for (UIView *subView in self.subviews) {
        NSLog(@"subview: %@", self.subviews);
        if([NSStringFromClass([subView class]) rangeOfString:@"Delete"].location != NSNotFound) {
            CGRect newFrame = subView.frame;
            newFrame.size.height = 87;
            subView.frame = newFrame;
        }
    }

    [UIView commitAnimations];
}

But self.subView only have two views i.e.

  • UITableViewCellContentView
  • UITableViewCellSeparatorView

How to get tableViewCell's delete button view in iOS 10+?

Edit

Here is my view hierarchy :

enter image description here

like image 314
Muhammad Umair Avatar asked Jul 13 '26 12:07

Muhammad Umair


2 Answers

For anybody who is struggling with the same problem.

In iOS10+ view hierarchy for UITableView's delete button has been changed. Now it comes under UITableView - UISwipeActionPullView - UISwipeActionStandardButton

enter image description here

So now instead of overriding custom UITableViewCell's layoutSubviews method, we need to iterate UITableView subviews in order to get UISwipeActionStandardButton. And I found tableView's willBeginEditingRowAtIndexPath delegate as an appropriate place,

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (UIView *subview in tableView.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
            if ([NSStringFromClass([subview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {
                CGRect newFrame = subview.subviews[0].frame;
                newFrame.size.height = 72;
                subview.subviews[0].frame = newFrame;

                //Other changes on this view can also be applied like
                subview.subviews[0].backgroundColor = [UIColor redColor];
                subview.subviews[0].layer.cornerRadius = 12;
                subview.subviews[0].layer.masksToBounds = YES;
            }
        }
    }
}
like image 198
Muhammad Umair Avatar answered Jul 15 '26 09:07

Muhammad Umair


Same code but works with Swift 5 and iOS 12+

 func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        for subview in tableView.subviews {
            if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView",
                let button = subview.subviews.first,
                NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton" {

                button.backgroundColor = .red
                button.layer.cornerRadius = 8
                button.layer.masksToBounds = true
                (button as? UIButton)?.contentHorizontalAlignment = .center
                (button as? UIButton)?.contentVerticalAlignment = .center
                (button as? UIButton)?.titleEdgeInsets = UIEdgeInsets(top: 6, left: 0, bottom: 0, right: 0)
                button.superview?.backgroundColor = UIColor(white: 0.97, alpha: 1)
                button.superview?.layoutIfNeeded()
            }
        }
    }
like image 20
UnRewa Avatar answered Jul 15 '26 08:07

UnRewa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!