微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AnnotationView 中的 UIButton - 如何保留图标和按钮

如何解决AnnotationView 中的 UIButton - 如何保留图标和按钮

我需要关于 annotationView 的帮助。我要做的是已经添加了图标,但是当我要单击图钉(图标)时,我希望在注释视图框架中看到该按钮,如下图所示:

enter image description here

点击后,我想看到这个白框和 uibutton:

enter image description here

我发现的方法添加var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 但是我不能使用它两次 - 你可以在我的代码中看到它。

这是我的代码

    func znajdzSzczytyNaMapie(_ szczyty: [Szczyt]) {
      for szczyt in szczyty {
        let annotations = MKPointAnnotation()
        annotations.title = szczyt.name
        annotations.subtitle = szczyt.opis
        annotations.coordinate = CLLocationCoordinate2D(latitude:
          szczyt.lattitude,longitude: szczyt.longtitude)
        mapView.addAnnotation(annotations)
      }
    }

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else { return nil }
        let annotationView = MKMarkerAnnotationView(annotation: annotation,reuseIdentifier: "MyMarker")
       // let identifier = "Gora"
       // var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) // The case is that obvIoUsly I cannot use let/var annotationView 2x times.
        let btn = UIButton(type: .detaildisclosure)
        annotationView.rightCalloutAccessoryView = btn
        switch annotation.title!! {
            case "Turbacz":
                annotationView.markerTintColor = UIColor(red: 0.86,green: 0.99,blue: 0.79,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "bald")
            case "example":
                annotationView.markerTintColor = UIColor(red: 0.80,green: 0.98,blue: 0.73,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "bear")
            default:
                annotationView.markerTintColor = UIColor.green
                annotationView.glyphImage = UIImage(named: "gora")
   } 
return annotationView
}

编辑: 现在我有

 func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else { return nil }
        //let annotationView = MKMarkerAnnotationView(annotation: annotation,reuseIdentifier: "MyMarker")
        let identifier = "identifier"
        guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView else { return nil }
        let btn = UIButton(type: .detaildisclosure)
        annotationView.rightCalloutAccessoryView = btn
        switch annotation.title!! {
            case "Turbacz":
                annotationView.markerTintColor = UIColor(red: 0.86,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "bear")
            default:
                annotationView.markerTintColor = UIColor.green
                annotationView.glyphImage = UIImage(named: "gora")
   } 
return annotationView
}

但不是我的图标(参见本主题中的第一张图片)和按钮,它显示了没有按钮视图的自定义注释图钉:

enter image description here

解决方法

看起来你的代码没有超出这一行,所以它返回了默认的注释视图。另外我误导你使用旧​​的出队方法

代替这一行;

guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView else { return nil }

试试这个,注意额外的 for: annotation 部分;

guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier,for: annotation) as? MKMarkerAnnotationView else { return nil }

但要使其正常工作,首先需要注册视图,然后再将注释添加到地图视图。

在调用 func znajdzSzczytyNaMapie(_ szczyty: [Szczyt])

之前添加以下代码行
mapView.register(MKMarkerAnnotationView.self,forAnnotationViewWithReuseIdentifier: "identifier")

在您了解其工作原理后,我建议您将标记注释视图子类化并在该类中进行自定义以清理代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?