Reputation: 13
So I have a map with tons of annotations. Each annotation has the default callout view attached with a button I created that supposedly takes the user to another view controller. This button works fine at first but for some annotations, it does not register touches unless I zoom in on the annotation or click the annotation again. I am very lost. Here is my code.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = MKAnnotationView()
guard let annotation = annotation as? LocationAnnotation else {return nil}
var identifier = ""
switch annotation.type {
case .nightclub:
identifier = "Nightclub"
case .hookahLounge:
identifier = "Hookah Lounge"
case .bar:
identifier = "Bar"
case .bowling:
identifier = "Bowling Alley"
case .arcade:
identifier = "Arcade"
case .pool:
identifier = "Pool"
}
if let dequedView = mapSF.dequeueReusableAnnotationView(withIdentifier: identifier) {
annotationView = dequedView
} else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
annotationView.canShowCallout = true
annotationView.isEnabled = true
let button = UIButton()
button.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
let image = UIImage(named: "go")
button.setImage(image, for: .normal)
annotationView.detailCalloutAccessoryView?.backgroundColor = UIColor.darkGray
annotationView.rightCalloutAccessoryView = button
}
And here is the callout accessory function which does work initially but does not work at random times.
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("true button tapped")
}
Again my issue is that print statement does not execute for various annotations. My console would print that statement out every time I press the button in the callout but for some other times it does not.I have no idea why. Any help would be appreciated as this is one of my last remaining bugs for my app.
Upvotes: 0
Views: 142
Reputation: 13
This is somewhat old for me now but I DID find a solution. In order to receive touches on the button in my callout, I had to simply add one line annotationView.isUserInteractionEnabled = false
. And my image in my callout button was interactive regardless of if the map was zoomed in or not at all times. What a weird scenario but it works swimmingly.
Upvotes: 1