I have a favourite button on a callout accessory with the toggle visually working by subclassing UIButton (This handles the image toggle). When I move the map around and I assume the view is redrawn the toggle will return (more often than not) to it's original untoggled state. Does anybody have any experience with this kind of development ? Thanks in advance.
You can handle this in your viewForAnnotation
method on the map. Assuming you have subclassed MKAnnotation
and are using reusable MKAnnotationView
s. In your annotation class add a flag to determine whether the annotation is selected or not and keep it updated. Then, in your viewForAnnotation
method...
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = yourMap.dequeueReusableAnnotationView(withIdentifier: "someIdentifier") as! YourCustomAnnotationClass
if annotation.isSelected {
// Setup your button here
}
return annotationView
}
Basically, if you are using reusable annotation views, you have to set up the view similar to how collection views work. Every time viewFor annotation
is called this code will run so some weird stuff can happen if you don't manually set it up properly every time or otherwise aren't careful with how the cell is reused.