【发布时间】:2016-12-17 01:34:58
【问题描述】:
我的问题是 annotationView.image = X 不起作用,我只是看不到特殊的 pin,如果我删除它只会变成紫色(pinTintColor) - 它变成红色..
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
for mall in malls {
let coords = CLLocation(latitude: mall.latitude, longitude: mall.longitude)
let annotation = MyAnnotation.init(coordinate: coords.coordinate, mall: mall, title: mall.name, subtitle: mall.adress)
self.mapView.addAnnotation(annotation)
}
}
而且我也无法将图像添加为附件视图。有什么问题?
extension commonMapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {return nil }
let annotationIdentifier = "restAnnotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = rightImage
annotationView?.canShowCallout = true
}
annotationView?.pinTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
annotationView?.image = UIImage(named: "malls.png")
return annotationView
}
感谢您的帮助,请原谅我的愚蠢..
【问题讨论】:
-
问题是你使用的是
MKPinAnnotationView。在旧的 iOS 版本中,您可以设置image,但在最近的版本中不能。使用MKAnnotationView。 -
另外,如果您成功地将先前的注释视图出列,请记住在
else子句中将其annotation设置为您的if语句。