【发布时间】:2020-01-11 13:15:44
【问题描述】:
我要做的是在地图视图 (MKMapView) 上显示自定义注释视图,这在应用程序中运行良好。 问题是当我在应用程序之间切换或在明暗模式之间切换时,自定义注释视图似乎恢复为默认注释引脚,如下所示。
部署目标:iOS 11
运行于:iPhone XS MAX iOS 13.3
Xcode 版本:11.3 (11C29)
Swift 版本:5.0
private func addAnnotations(_ places : [QPPlaceDM]) {
mapView.removeAnnotations(myAnnotations)
var annotations = [MKPointAnnotation]()
for item in places {
let annotation = QPCustomAnnotaion()
annotation.title = item.name
annotation.coordinate = item.coordinates
annotation.image = item.category.pinImage
annotations.append(annotation)
}
myAnnotations = annotations
mapView.addAnnotations(myAnnotations)
}
.......
extension QPMainVC : MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is QPCustomAnnotaion) {
return nil
}
let annotationID = "AnnotationId"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
annotationView?.canShowCallout = true
}
else {
annotationView?.annotation = annotation
}
let myCustomAnnotation = annotation as? QPCustomAnnotaion
annotationView?.image = myCustomAnnotation?.image
annotationView?.displayPriority = .required
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotationLocation = view.annotation?.coordinate else { return }
var indexOfAnnotation = 0
for (index , item) in myAnnotations.enumerated() {
if annotationLocation.latitude == item.coordinate.latitude{
indexOfAnnotation = index
break
}
}
let indexPathOfMagorCell = IndexPath(row: indexOfAnnotation, section: 0)
placesCollection.scrollToItem(at: indexPathOfMagorCell, at: .centeredHorizontally, animated: true)
}
}
【问题讨论】:
-
您是否尝试过在
if !(annotation is QPCustomAnnotaion) { return nil }中设置断点以确保在切换回应用程序时不会调用此行?确定是否是这种情况应该可以帮助您缩小范围。
标签: ios swift xcode mapkit mapkitannotation