添加 viewForAnnotation 时不显示注释标题
Annotation title not appearing when adding viewForAnnotation
我添加了这个:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
if let title = annotation.title{
if title! == "P"{
pinView.pinTintColor = UIColor.yellow
}
else{
pinView.pinTintColor = UIColor.gray
}
}
return pinView
}
但是当我这样做时,当我点击注释时我再也看不到我的标题了,知道为什么吗?
您需要在 pinView
上添加 canShowCallout
。如果 canShowCallout
属性 的值为 true
,当用户点击选定的注释视图时,将显示标准标注气泡。标注使用关联注释 object.
中的标题和副标题文本
因此,只需将以下行添加到您的 viewFor
函数中,您就会看到标题。
pinView.canShowCallout = true
我添加了这个:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
if let title = annotation.title{
if title! == "P"{
pinView.pinTintColor = UIColor.yellow
}
else{
pinView.pinTintColor = UIColor.gray
}
}
return pinView
}
但是当我这样做时,当我点击注释时我再也看不到我的标题了,知道为什么吗?
您需要在 pinView
上添加 canShowCallout
。如果 canShowCallout
属性 的值为 true
,当用户点击选定的注释视图时,将显示标准标注气泡。标注使用关联注释 object.
因此,只需将以下行添加到您的 viewFor
函数中,您就会看到标题。
pinView.canShowCallout = true