如何使用 XIB 制作自定义 MKAnnotationView [英] How to make a custom MKAnnotationView with XIB

查看:30
本文介绍了如何使用 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- 创建一个 UIView 的视图子类 with xib say CallView

1- Create a view subclass of UIView with xib say 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆