Я хочу иметь собственный MKAnnotationView.Я создал xib-файл в IB и установил для него класс MyAnnotationView.
class MyAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var busIcon: UIImageView!
}
Вот как выглядит xib - у него есть textLabel и busIcon:
Я использую метод делегата viewFor annotation
для создания представлений для всех аннотаций:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
if (annotation is MKUserLocation) {
return nil
} else {
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MyAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationIdentifier") as? MyAnnotationView {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
} else {
// if no views to dequeue, create an Annotation View
let av = MyAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
annotationView.canShowCallout = true // callout bubble
annotationView.image = UIImage(named: "Delivery")
annotationView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
}
return annotationView
}
}
annotationView.image = UIImage(named: "Delivery")
&
AnnotationView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
предназначены только для проверки работоспособности кода и отображения образца карты на карте, поскольку они используют стандартные свойства, унаследованные от MKAnnotationView.
Я не знаю, как заставить метод viewFor annotation
использовать созданный мной XIB.Может ли кто-нибудь помочь мне с этим?Я искал решение, но нашел только что-то соответствующее в Obj C.
Спасибо!