【问题标题】:Callout View doesn't close标注视图未关闭
【发布时间】:2020-12-29 17:01:30
【问题描述】:

我点击标注视图显示的注释图钉,但是当我选择注释图钉时,前一个视图不会关闭。

视图控制器

 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()
        }
    }
}

enter image description here

出了什么问题

【问题讨论】:

    标签: xcode xib callouts


    【解决方案1】:

    您不要在mapView(mapView:didSelect view:) 内添加带有view.addSubview(calloutView) 的标注视图,这听起来是个坏习惯。你应该使用mapView(mapView:viewFor annotation:)

    您还试图删除 annotationView 的子视图,而不是它本身。

    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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多