Swift - 更改注释图像不起作用

问题描述 投票:3回答:2

我已经按照其他堆栈帖子和教程将我的注释图像更改为自定义图像,但它似乎不起作用。不会出现错误或运行时错误,只是注释图像不会改变。

顺便说一下,我在annotationView!.image = UIImage(named: "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"))
}
ios swift mkmapview mkannotation
2个回答
9
投票

问题是你正在使用MKPinAnnotationView。如果您使用MKAnnotationView,您将看到您的图像。

在过去(例如iOS 8),设置imageMKPinAnnotationView似乎工作正常,但在iOS 9及更高版本中,它使用一个引脚,无论如何(对于一个名为MKPinAnnotationView的类来说,这不是完全不合理的行为; lol)。

使用MKAnnotationView避免了这个问题。


1
投票

在返回之前调用它

if let annotationView = annotationView {
    // Configure your annotation view here
    annotationView.image = UIImage(named: "RaceCarMan2png.png")
}

return annotationView

并检查断点是否在if内是可以的

© www.soinside.com 2019 - 2024. All rights reserved.