如何防止自定义 MKAnnotationView 恢复使用默认颜色和字形图像?

How to prevent custom MKAnnotationViews from reverting back to using the default color and glyph image?

我在 UITabBarController 的单独选项卡上有几个 MKMapView。每个地图都为 MKAnnotations 和 MKClusterAnnotations 使用自定义 MKAnnotationViews。自定义 MKAnnotationView 为带有星形字形的绿色,如果用于群集,则为带有数字的绿色。

问题是我的自定义 MKAnnotationViews 随机恢复为带有 pin 字形的默认红色外观。当我放大时,视图将开始重绘并且是正确的。我无法始终如一地重现该问题,但当我离开某个选项卡然后 return.

时似乎会发生这种情况

有没有一种方法可以强制重新加载地图视图,类似于集合视图上的 reloadData()?我的 viewForAnnotation func 不应该 return nil 所以我不知道这是怎么发生的。

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
     var view = mapView.dequeueReusableAnnotationView(withIdentifier: "CustomAnnotationView") as? CustomAnnotationView
     if view == nil {
         view = CustomAnnotationView(annotation: annotation, reuseIdentifier: "CustomAnnotationView")
     } else {
         view?.annotation = annotation
     }
     return view

     // code for the cluster annotations redacted
 }

问题已解决。显然,我需要在集群子类的 prepareForReuse 函数中设置标记的样式。

override func prepareForReuse() {
    super.prepareForReuse()

    style()
}