2

Is there a way to change the size of the delete button, for slide-to-delete in a tableView? I made my own borders for the cell, so the delete button extends into the border like this:

enter image description here

Relevant code is below:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in

            self.reminders.remove(at: indexPath.row)
            tableView.reloadData()

        }

        deleteAction.backgroundColor = .red

        let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
        return configuration
    }
user3628240
  • 877
  • 1
  • 23
  • 41

1 Answers1

1

Try this Updated answer

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

Height of table view cell

Set height of table view constraint:

heightOfTableViewConstraint = NSLayoutConstraint(item: self.tableView, attribute: .height, relatedBy: .equal, toItem: containerView, attribute: .height, multiplier: 0.0, constant: 1000)
containerView.addConstraint(heightOfTableViewConstraint)

Call tableView.layoutIfNeeded(), and when completed, look for the visible cells, sum up their height, and edit the heightOfTableViewConstraint:

UIView.animate(withDuration: 0, animations: {
    self.tableView.layoutIfNeeded()
    }) { (complete) in
        var heightOfTableView: CGFloat = 0.0
        // Get visible cells and sum up their heights
        let cells = self.tableView.visibleCells
        for cell in cells {
            heightOfTableView += cell.frame.height
        }
        // Edit heightOfTableViewConstraint's constant to update height of table view
        self.heightOfTableViewConstraint.constant = heightOfTableView
}
Faizul Karim
  • 333
  • 2
  • 13
  • 1
    Is there a way to get the height of each cell? I used UITableViewAutomaticDimension for row height, so each cell is slightly different. – user3628240 Apr 29 '18 at 22:50
  • 1
    Can you explain what the use of subview is for? I'm not sure I completely follow your code – user3628240 May 01 '18 at 08:21