【问题标题】:Not able to display left accessory view for an annotation in swift无法快速显示注释的左侧附件视图
【发布时间】:2017-02-14 06:37:21
【问题描述】:

此代码用于初始化注释的视图,但右侧的辅助视图在地图中不可见。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: "Map")


    if view == nil {
        view = MKAnnotationView(annotation: annotation, reuseIdentifier: "Map")
        view?.canShowCallout = true
        let btn = UIButton(type: .detailDisclosure)
        view?.rightCalloutAccessoryView = btn

    }

    view?.annotation = annotation
    return view
}

我不知道是什么问题。

【问题讨论】:

  • 您正在使用rightCalloutAccessoryView 并要求左转
  • 感谢您的指出。 @SachinVas
  • @RohitDulam CalloutAccessoryView 是在您 tap/select 之前显示的注释。
  • 是的,但是在我触摸左侧附件视图后也没有显示它。 @NiravD
  • @RohitDulam 您需要开始接受有助于您从不接受任何答案的答案。请查看stackoverflow.com/help/someone-answersmeta.stackexchange.com/questions/5234...。这将为您和帖子的作者增加声誉。

标签: ios swift apple-maps


【解决方案1】:

使用这个

  func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

  if annotation is MKUserLocation {
    return nil
 }

  let reuseId = "AddPin"
  let pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
  if pinView == nil 
  {
    pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView!.canShowCallout = true
    pinView!.animatesDrop = true
    pinView!.pinColor = .Red

    pinView!. leftCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
   } else {
    pinView!.annotation = annotation
  }
   return pinView
  }

【讨论】:

  • 你是否参与制作
  • 是的,图钉正在地图上显示。
  • 将此从左到右更改为 pinView!。 rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) 作为 UIButton。并查看正确的标注是否正在制作
  • 至少你的代码应该在左侧显示按钮,但这也没有发生,所以你的代码没有解决我的问题。
【解决方案2】:

请使用它并检查它

    let reuseAnno = "test"

    var anno = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseAnno)
    if anno == nil {

        anno = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseAnno)
        anno?.image = UIImage(named: "imagename.png")
        anno?.canShowCallout = true
    }

    let customButton = UIButton(type: .DetailDisclosure)
    customButton.addTarget(self, action: #selector(ViewController.yourFuncName), forControlEvents: .TouchUpInside)
    anno?.rightCalloutAccessoryView = customButton
    return anno

【讨论】:

  • 谢谢,但该解决方案似乎不起作用。 @Sunil Prajapathi
  • 您想在左侧还是右侧显示您的详细信息披露按钮?
  • 地图上的位置有标注吗?
  • 是的,我收到了注释。
  • 可以发模拟器的图片吗?
猜你喜欢
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多