Swift-更改注释图像不起作用 [英] Swift - Changing Annotation Image Not Working
问题描述
我已经按照其他堆栈文章和教程将注释图像更改为自定义图像,但是它似乎不起作用.没有错误或运行时错误出现,只是注释图像没有改变.
I have followed other stack posts and tutorials in changing my annotations image to a custom one, but it does not seem to work. No errors or runtime errors appear, it's just that the annotation image does not change.
顺便说一句,我在行annotationView!.image = UIImage(named: "RaceCarMan2png.png")
上设置了一个断点,它表明该行正在被调用,但是什么也没发生.我将衷心感谢您的帮助.谢谢.
Just by the way I set a break point on the line annotationView!.image = UIImage(named: "RaceCarMan2png.png")
and it shows that the line is being called, but yet nothing happens. I would really appreciate your help. Thanks.
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
,将看到您的图像.
The issue is that you're using MKPinAnnotationView
. If you use MKAnnotationView
, you will see your image.
过去(例如,iOS 8),设置MKPinAnnotationView
的image
似乎可以正常工作,但是在iOS 9和更高版本中,无论使用哪种引脚(对于称为类的类来说,这都不是完全不合理的行为) MKPinAnnotationView
;哈哈).
In the past (e.g. iOS 8), setting the image
of MKPinAnnotationView
seemed to work fine, but in iOS 9 and later, it uses a pin, regardless (which is not entirely unreasonable behavior for a class called MKPinAnnotationView
; lol).
使用MKAnnotationView
可以避免此问题.
这篇关于Swift-更改注释图像不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!