Sergey Udalov
Sergey Udalov

Reputation: 91

MKMapKit. MKMapViewDelegate doesn't respond. SwiftUI

I'm trying to implement tap on rightCalloutAccessoryView. I've got custom MkAnnotationView:

class WonderViewAnnotation: MKAnnotationView, Identifiable {
    override var annotation: MKAnnotation? {
        willSet {
            guard let artwork = newValue as? Annotation else {
                return
            }
            calloutOffset = CGPoint(x: 0, y: 5)
            let imageCustom: UIImage = .mapPin
            image = imageCustom
            let mapsButton = UIButton(type: .detailDisclosure)
            rightCalloutAccessoryView = mapsButton
            let detailLabel = UILabel()
            detailLabel.numberOfLines = 0
            detailLabel.font = detailLabel.font.withSize(12)
            detailLabel.text = artwork.subtitle
            detailCalloutAccessoryView = detailLabel
        }
    }
}

Which I register in MapView of cause with set as delegate.

struct MapViewRepresentable: UIViewRepresentable {
    @ObservedObject var mapData: MapViewModel
    
    func makeCoordinator() -> Coordinator {
        return MapViewRepresentable.Coordinator(vm: mapData)
    }
    
    func makeUIView(context: Context) -> MKMapView {
        
        let configuration = MKStandardMapConfiguration()
        configuration.pointOfInterestFilter = MKPointOfInterestFilter(including: [])
        mapData.mapView.showsUserLocation = true
        mapData.mapView.register(WonderViewAnnotation.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        mapData.mapView.delegate = context.coordinator
        mapData.mapView.preferredConfiguration = configuration
        return mapData.mapView
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) {}

And I see fully custom pin view on the map. But tap gesture doesn't work.

Print doesn't display in console:

extension MapViewRepresentable.Coordinator: MKMapViewDelegate {
    func mapView(
        _ mapView: MKMapView,
        annotationView view: MKAnnotationView,
        calloutAccessoryControlTapped control: UIControl
    ) {
        print("tapped")
        if view.isKind(of: WonderViewAnnotation.self) {
            vm.selectedItem =  view.annotation
        }
    }
}

Upvotes: 0

Views: 22

Answers (0)

Related Questions