ios - 如何在 Ios 中添加 customView xib 作为注释?

标签 ios mapkit swift4

当我使用 googlemaps 时,我所要做的就是

    let marker = GMSMarker()
    marker.position = postions.target
    marker.title = "TEST"
    marker.map = self.mapView
    let myCustomView:CustomView = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView
    marker.iconView = myCustomView

但如果我使用 MAPKIT,我将如何实现这一目标。? Mycustomview 是我创建的 xib View 文件。

最佳答案

试试像这样把nib View 的对象添加到annotation View

 func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    if annotation is MKUserLocation == true
    {

        return nil
    }


    let senderAnnotation = annotation as! MyAnnotation

    let pinReusableIdentifier = senderAnnotation.pinColor.rawValue

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier)

    if annotationView == nil
    {

        annotationView = MKAnnotationView(annotation: senderAnnotation,                                                                                                reuseIdentifier: pinReusableIdentifier)
          annotationView!.canShowCallout = true



    }

  let customView = (Bundle.main.loadNibNamed("directOrderView", owner: self, options: nil))?[0] as! directOrderView;

  var calloutViewFrame = customView.frame;
  calloutViewFrame.origin = CGPoint(x:-calloutViewFrame.size.width/2 + 30,y: -calloutViewFrame.size.height);
  customView.frame = calloutViewFrame;

  annotationView?.addSubview(customView)

  return annotationView

  }

关于ios - 如何在 Ios 中添加 customView xib 作为注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887717/

相关文章:

iphone - 删除 iPhone 上的 Mapkit 覆盖

json - Swift 4 可编码;如何使用单个根级 key 解码对象

iphone - iOS 在不打开 Instagram 应用程序的情况下将照片发布到 Instagram

iphone - 通过标签栏访问 MKMapView

ios - Xcode 7 beta 中缺少 AVFoundation 框架

ios - 检测结束拖动/缩放 MKMapView

ios - 使用 BSImagePicker Swift4 在图库中显示刚刚选定的图像

ios11 - Swift 4 按钮淡入、淡出

ios - 如何在 ios 中发送带有链接和图像的自定义推文?

ios - textField resignFirstResponder 消失 UITableView header