m1234 m1234 - 5 months ago 21
iOS Question

Hiding button in annotation callout Swift

I am wondering how I can hide and show a button in an annotation callout?

I set up my annotation view to have a left and a right button, each doing a separate task. The user can add an annotation by selecting a location from a table or long press.

I want the left button to be accessible and the right button to be hidden when the annotation is added via selection from table and the left button to be hidden the the right button to be accessible when the annotation is added via long press.

I have the buttons in the callout working but I can't figure how how to hide/show them. Any help is much appreciated!

Answer

Don't use a generic MapAnnotation object. Create a custom object that conforms to the MKAnnotation protocol and give it an Enum property that has a different value if it's added from your table or from a long press.

In your viewForAnnotation method, cast the annotation object to your custom type and check the property before deciding which callouts to add. (You'll need special-case code for the user location, as always.)