Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 stop default behaviour of swipe UITableViewCell

In my App, one screen has table view and displaying "Edit" and "Delete" buttons on swipe UITableViewCell.

All is working good but issue is in default animation of UITableViewCell in iOS 11.

I know some behaviour and funda are changed in iOS 11. When you are continue pulling to swipe your cell then editActionsForRowAt method will be called automatically.

Some times it's good but when you added more then one button on table swipe then looking ugly behaviour.

Look at below, Default behaviour of swipe cell in iOS 10 and iOS 11.

iOS 10:

enter image description here

iOS 11:

enter image description here

You can see in iOS 11 if I pull to swipe then automatically editActionsForRowAt method os call and display alert message.

My question: Is there any why to stop this behaviour of iOS 11 and code for that?

Because if your table has more then 2 button then it's looking ugly.

like image 241
iPatel Avatar asked Oct 11 '17 12:10

iPatel


1 Answers

This seems to do what you are looking for. However the animation isn't exactly the same as in iOS 10 but it eliminates the full swipe, and still looks good.

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let deleteAction = UIContextualAction.init(style: UIContextualAction.Style.destructive, title: "Delete", handler: { (action, view, completion) in
        //TODO: Delete
        completion(true)
    })

    let editAction = UIContextualAction.init(style: UIContextualAction.Style.normal, title: "Edit", handler: { (action, view, completion) in
        //TODO: Edit
        completion(true)
    })

    let config = UISwipeActionsConfiguration(actions: [deleteAction, editAction])

    config.performsFirstActionWithFullSwipe = false
    return config
}

The key line is config.performsFirstActionWithFullSwipe = false as this will disable the full swipe.

The above code will override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) on iOS 11 but your editActionsforRowAt will still be used for sub iOS 10 devices.

like image 154
Micah Wilson Avatar answered Oct 14 '22 05:10

Micah Wilson