SwiftHacker SwiftHacker - 1 year ago 120
iOS Question

How to hide Bar Button Items when UITableView is in edit mode? (Swift)

Title essentially says it all.

I have a

and I want the
item to disappear while the
is in edit mode. Unfortunately, all of the answers that I have found so far suggest setting the button to nil... which won't work for me because I do not want to get rid of the button and the reference to it, just hide it while the
is in edit mode.

What I'm having trouble figuring out what to do, then, is:

  1. Detect when the
    has entered editing mode

  2. Hiding the
    (not removing it entirely)

  3. Detect when the
    has left editing mode (so the button can reappear)

Any help would be appreciated, thank you!

Answer Source

You can use an optional datasource method to detect when a row is being edited, tableView(_:canEditRowAtIndexPath:)

And inside that method, you can either hide or disable the bar button item (disabling is probably the friendlier thing to do, in terms of the UI and code). There is no hidden property on a bar button, so to properly hide it means you potentially do some grody coding to temporarily remove or make it disappear.

Anyways, I suggest something like:

func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    self.navigationItem.rightBarButtonItem.enabled = false
    return true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download