0

I am using a Edit/Done button to move an MKMapView upwards when Edit mode is selected, with the intention of display a message in an imageView at the bottom of the screen. My understanding is to change the function of this button I must use override func setEditing().

While I can get it to change from Edit to Done mode once, with the corresponding title change, I can never get it to change back to Edit mode. The result being when you just press "Done" over and over it remains "Done" and keeps moving the MKMapView up.

I want this to operate as a toggle but the process escapes me for some reason:

    override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)

if editing {
    print("Are we editing NOW: \(isEditing)")
    mapView.frame.origin.y = -24
    editButtonItem.title = "Done"
} else {
    print("Are we editing: \(isEditing)")
    mapView.frame.origin.y = 64
    editButtonItem.title = "Edit"
}
super.setEditing(editing, animated: true)

My attempted implementations of "isEditing" and "isEnabled" within my if/else statement haven't worked so far. What am I missing?

EDIT:

Well I came up with this approach, which works but seems clunky.

    override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: true)

    if editing {
        print("Are we editing NOW: \(isEditing)")
        mapView.frame.origin.y = -24
        editButtonItem.title = "Done"
    } else {
        print("Are we editing: \(isEditing)")
        mapView.frame.origin.y = 64
        editButtonItem.title = "Edit"
    }
    super.setEditing(editing, animated: true)
}

I am having to use 64 as a value to get the mapView to return to the bottom the super view, not exactly sure why.

Center view controller is the one in question

Pigpocket
  • 449
  • 5
  • 24

1 Answers1

1

First, do not create your own button for the Edit/Done button. UIViewController provides this for you using the editButtonItem property.

I don't know how to use it in a storyboard but in code you add the following line in your viewDidLoad method:

navigationItem.rightBarButtonItem = editButtonItem

This button is already setup to call the setEditing(_:animated:) method and to toggle the button title between Edit and Done.

So all you need to do is to override the setEditing(_:animated:) method.

Your implementation is close. Only call super.setEditing once at the beginning and don't try to set the button's title.

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: true)

    if editing {
        print("Are we editing NOW: \(isEditing)")
        mapView.frame.origin.y = -24
    } else {
        print("Are we editing: \(isEditing)")
        mapView.frame.origin.y = 64
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • That did it! I had already set the editButtonItem in viewDidLoad, actually. I felt like I tried the remainder of this code before and it didn't work but anyway it does now--thank you. – Pigpocket Jan 16 '18 at 05:33
  • Glad to help. Please don't forget to indicate that your question has been successfully answered by clicking the checkmark to the left of the answer. You may wish to do the same for some of your other questions that have suitable answers. – rmaddy Jan 16 '18 at 05:35