jonask jonask - 6 months ago 27
Swift Question

My annotation pins are hidden

I was making some callout accessories in the annotation function, and suddenly my annotation pins was hidden? They are only hidden, cause if i tap a place where i know there is a annotation, the annotationview will come up. The worst part is, i can't just regret (Cmd+Z), cause my computer went down while the project was running. I can't figure out what i changed, that will hide the pins.



func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

if annotation is MKUserLocation {
return nil
}

let identifier = "MyCustomAnnotation"

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true

} else {

annotationView!.annotation = annotation

}

let image = UIImage(named: "advance.png")
let button = UIButton(type: .DetailDisclosure)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
annotationView?.rightCalloutAccessoryView = button

let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50))
detailImage.image = UIImage(data: fishimages!)
annotationView?.leftCalloutAccessoryView = detailImage


return annotationView
}

Answer

Not hidden, but transparent!

When you allocate your MKAnnotationView() (in the leg where you cannot de-queue an existing one), you are not setting the view's .image - therefore it is a blank view, not a pin. Consider using

let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 

which will always return a non-nil pin.

Comments