user2779581 user2779581 - 8 months ago 29
Swift Question

How to get Delete functionality similar to iOS's native Mail app

I have a delete button that becomes visible when swiping, but I really like what happens in the mail app, where if you keep swiping, the delete button will be used, deleting the item.
I've seen a lot of tutorials on implementing items using

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?

but I can't figure out what to use to make the button expand to the full width of the table cell. Any thoughts?

A look at what I want to implement

edit: I'd settle for just disabling swiping once the delete key has been revealed, I don't like how you can swipe the table cell to see the underlying background.


This is possible using UIScrollView paging.

Create a UIScrollView and add it as a subview to your UITableViewCell (Also add size constraint to make the scrollview same size as it's parent bounds)

Now set this code for the scrollview:

//set scroll view
scrollview.delegate = self;
scrollview.pagingEnabled = true;
scrollview.showsHorizontalScrollIndicator = false;
scrollview.showsVerticalScrollIndicator  = false;
scrollview.contentSize.width = scrollview.bounds.width * 2.0;

//create delete swipe view
let deleteButton = UIView(frame: CGRectMake(scrollview.bounds.width,0,scrollview.bounds.width * 2.0,scrollview.bounds.height))
deleteButton.backgroundColor = UIColor.redColor()

let deleteLabel = UILabel(frame: CGRectMake(0,0,scrollview.bounds.width * 0.3,scrollview.bounds.height))
deleteLabel.textColor = UIColor.whiteColor()
deleteLabel.text = "Delete"
deleteLabel.contentMode = .Center
deleteLabel.textAlignment = .Center

now the delegate function:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    if scrollView.contentOffset.x == 0{

    let confirmDelete = UIAlertController(title: "Are you sure you want to delete?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    confirmDelete.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive, handler: { (_) -> Void in
       //Delete confirmed


    confirmDelete.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
        scrollView.contentOffset.x = 0;

    rootViewController.presentViewController(confirmDelete, animated: true, completion: nil);

this will create the next effect:

enter image description here