如何在iOS中添加customView xib作为注释? [英] How to add a customView xib as annotation in Ios?

查看:77
本文介绍了如何在iOS中添加customView xib作为注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

when i am using googlemaps all I have to do is

    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视图文件.

but if i am using MAPKIT, how will I achieve this.? Mycustomview is a xib view file i created.

推荐答案

尝试像这样将笔尖视图的对象添加到注释视图

Try to add the object of the nib view to the annotation view like this

 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中添加customView xib作为注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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