Reputation: 1
I tap to an annotation pin the callout view shows but while I select annotation pin the previous View doesn't close.
View Controller
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// 1
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
// 2
let pimAnnotation = view.annotation as! AnnotationPin
let views = Bundle.main.loadNibNamed("CalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! CalloutViewController
calloutView.titleXib.text = pimAnnotation.title
calloutView.subtitleXib.text = pimAnnotation.subtitle
calloutView.imageXib.image = pimAnnotation.image
// 3
calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
view.addSubview(calloutView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
if view.isKind(of: CalloutAnnotationView.self)
{
for subview in view.subviews
{
subview.removeFromSuperview()
}
}
}
what is going wrong
Upvotes: 0
Views: 53
Reputation: 9923
You don't add callout view with view.addSubview(calloutView)
inside mapView(mapView:didSelect view:)
, that sound like a bad practice. You should use mapView(mapView:viewFor annotation:)
for that.
You are also trying to remove the subviews of the annotationView, not itself.
Example for using mapView(mapView:viewFor annotation:)
:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is StoreAnnotation else { return nil }
var annotationView: MapAnnotationView?
if #available(iOS 11.0, *) {
annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView", for: annotation) as? MapAnnotationView
} else {
annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView") as? MapAnnotationView
if annotationView == nil {
annotationView = MapAnnotationView(annotation: annotation, reuseIdentifier: "annotationView")
}
}
//configure annotation view...
annotationView?.delegate = self
return annotationView
}
Upvotes: 0