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 me
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()
}
}
}
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
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;
}
}
}
}
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
}
}
}
}
Swift 5, this 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
}
}
}