【问题标题】:Why is my MKAnnotationView not showing?? - Swift为什么我的 MKAnnotationView 没有显示? - 斯威夫特
【发布时间】:2026-02-22 14:20:02
【问题描述】:

我的注释有效,但只要我添加此代码,注释就会停止出现。我做错了什么?

mapview.delegate = self

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

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
        if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView") }

        if annotation.subtitle == "Innovation" {            annotationView?.backgroundColor = UIColor(named: "Innovation")
        } else if annotation.subtitle == "Art" {            annotationView?.backgroundColor = UIColor(named: "Art")
        } else if annotation.subtitle == "Entertainment" {  annotationView?.backgroundColor = UIColor(named: "Entertainment")
        } else if annotation.subtitle == "Networking" {     annotationView?.backgroundColor = UIColor(named: "Networking")
        } else if annotation.subtitle == "Sale" {           annotationView?.backgroundColor = UIColor(named: "Sale")
        } else if annotation.subtitle == "Sports" {         annotationView?.backgroundColor = UIColor(named: "Sports")
        } else if annotation.subtitle == "Food & Drink" {   annotationView?.backgroundColor = UIColor(named: "Food & Drink")
        } else if annotation.subtitle == "Community" {      annotationView?.backgroundColor = UIColor(named: "Community")
        } else if annotation.subtitle == "Spiritual" {      annotationView?.backgroundColor = UIColor(named: "Spiritual") }
        else { annotationView?.annotation = annotation}

        return annotationView
    }
  let myCoordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
             let mapAnnotation = MKPointAnnotation()
                 mapAnnotation.title = title
                 mapAnnotation.subtitle = color
                 mapAnnotation.coordinate = myCoordinate
                 self.mapView.addAnnotation(mapAnnotation)

** 我没有自定义注释视图,因为我只想改变气球别针的颜色 **

【问题讨论】:

    标签: swift annotations mkmapview


    【解决方案1】:

    您需要设置MapView的delegate,并将注解注册到具有相同标识符的mapview中。

    试试

    Map.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: "SomeRandomIdentifier")
    

    希望能解决问题。

    【讨论】:

    • 是的!抱歉忘记添加了
    • 你是否将注解注册到具有相同标识符的mapview中?
    • Map.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: "SomeRandomIdentifier") 希望能解决问题。
    • 谢谢,我试试
    最近更新 更多