如何为 MKAnnotationView 使用自定义 XIB?

How to use a custom XIB for MKAnnotationView?

我想使用自定义 XIB 而不是 MKAnnotation View,但我找不到任何可以帮助我的在线资源。 需要明确的是,我不想要标注,我不想用图像代替 pin,我想用 xib 完全替换 pin。 作为参考,我希望我的图钉看起来像这样 -

https://i.stack.imgur.com/gbdFI.png

这将是图钉设计,中间的图像会为所有用户更改。 我从来没有在 MKMapView 上工作过,我只是在看了一些关于它的教程后在地图上显示了 MKPinAnnotationView。 谁能指导我如何实现这一目标

编辑: 使用 Alexander Nikolaychuk 的 link,我能够使用 XIB 创建 MKPinAnnotationView。

作为参考,这是对我有用的代码:

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 = "CustomPinAnnotation"
            let nibName = "CustomPinAnnotation"
            let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! CustomPinAnnotation
            var annotationView: CustomPinAnnotation?
            
            // if there is a view to be dequeued, use it for the annotation
            if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? CustomPinAnnotation {
                
                if dequeuedAnnotationView.subviews.isEmpty {
                    dequeuedAnnotationView.addSubview(viewFromNib)
                }
                annotationView = dequeuedAnnotationView
                annotationView?.annotation = annotation
            } else {
                
                // if no views to dequeue, create an Annotation View
                let av = CustomPinAnnotation(annotation: annotation, reuseIdentifier: annotationIdentifier)
                av.addSubview(viewFromNib)
                annotationView = av     // extend scope to be able to return at the end of the func
            }
            
            // after we manage to create or dequeue the av, configure it
            if let annotationView = annotationView {
                annotationView.canShowCallout = true                                    // callout bubble
                annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                annotationView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
                
                let customView = annotationView.subviews.first as! CustomPinAnnotation
                customView.frame = annotationView.frame
                customView.profilePicImageView.image = UIImage(named: "dummy4")
            }
            return annotationView
        }
    }

现在,我的应用中还剩下 2 个问题

  1. 我有我想在地图上以数组模式显示的所有数据。如何使用我的模式中的个人资料图片 URL 并将其显示到注释视图中的 imageView 上?由于 viewFor 注释不会迭代并且没有任何索引值,因此我很难在这里传递我的模态。

  2. 尽管我的 xIB 看起来像这样: https://i.stack.imgur.com/Wg4ZU.png

    在 MapView 上看起来像这样:https://i.stack.imgur.com/e7CAr.jpg

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: MKPinAnnotationView?                           


            if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationIdentifier") as? MKPinAnnotationView {
                //Here you can setup 
                annotationView = dequeuedAnnotationView
                annotationView?.annotation = annotation
            }

    }

您无需自行初始化。它使用自定义 CollectionViewCells。尝试使用这个例子。 (这是一个委托函数,仅供参考)