我已经按照其他堆栈帖子和教程将我的注释图像更改为自定义图像,但它似乎不起作用.不会出现错误或运行时错误,只是注释图像不会改变. 顺便说一下,我在一行annotationView!.image = UIIm
顺便说一下,我在一行annotationView!.image = UIImage(名字:“RaceCarMan2png.png”)上设置一个断点,它显示该行被调用,但没有任何反应.我将衷心感谢您的帮助.谢谢.
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "MyCustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView!.image = UIImage(named: "RaceCarMan2png.png")
} else {
annotationView!.annotation = annotation
}
configureDetailView(annotationView!)
return annotationView
}
func configureDetailView(annotationView: MKAnnotationView) {
annotationView.detailCalloutAccessoryView = UIImageView(image: UIImage(named: "url.jpg"))
}
问题是你正在使用MKPinAnnotationView.如果您使用MKAnnotationView,您将看到您的图像:
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
private func addAnnotation(coordinate coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = title
annotation.subtitle = subtitle
mapView.addAnnotation(annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let identifier = "CustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "star.png")! // go ahead and use forced unwrapping and you'll be notified if it can't be found; alternatively, use `guard` statement to accomplish the same thing and show a custom error message
} else {
annotationView!.annotation = annotation
}
return annotationView
}
...
}
产量:

在过去(例如iOS 8),设置MKPinAnnotationView的图像似乎工作正常,但在iOS 9.x中,似乎无论如何都使用pin.对于名为MKPinAnnotationView的类而言,这并非完全不合理的行为,并且使用MKAnnotationView可以避免此问题.
