如何解决AnnotationView 中的 UIButton - 如何保留图标和按钮
我需要关于 annotationView 的帮助。我要做的是已经添加了图标,但是当我要单击图钉(图标)时,我希望在注释视图框架中看到该按钮,如下图所示:
点击后,我想看到这个白框和 uibutton:
我发现的方法是添加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
}
但不是我的图标(参见本主题中的第一张图片)和按钮,它显示了没有按钮视图的自定义注释图钉:
解决方法
看起来你的代码没有超出这一行,所以它返回了默认的注释视图。另外我误导你使用旧的出队方法
代替这一行;
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 举报,一经查实,本站将立刻删除。