如何使用XIB制作自定义MKAnnotationView

如何使用XIB制作自定义MKAnnotationView

本文介绍了如何使用XIB制作自定义MKAnnotationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个自定义的MKAnnotationView.我已经在IB中创建了一个xib文件,并将其类设置为MyAnnotationView.

I want to have a custom MKAnnotationView. I've created a xib file in IB and set its class to 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链接:

Here's how the xib looks like - it has a textLabel and a busIcon linked:

我正在使用viewFor annotation委托方法来创建所有注释的视图:

I'm using the viewFor annotation delegate method to create views for all annotations:

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继承的标准属性.

are there just to check if the code is working and display a sample view on the map, as they use the standard properties inherited from MKAnnotationView.

我不知道如何使viewFor annotation方法使用我创建的XIB.有人可以帮我吗?我搜索了解决方案,但只在Obj C中找到了相关内容.

I don't know how to make the viewFor annotation method use the XIB I have created. Could anyone please help me with that? I searched for the solution, but only found something relevant in Obj C.

谢谢!

推荐答案

1-用xib说CallView

2-内部viewforAnnotation

let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "id")
let customView = Bundle.main.loadNibNamed("CallView", owner: self, options: nil).first! as! CallView
// here configure label and imageView
annotationView.addSubview(customView)

这篇关于如何使用XIB制作自定义MKAnnotationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:50