【问题标题】:UIButton inside MKPinAnnotationView with image带有图像的 MKPinAnnotationView 内的 UIButton
【发布时间】:2015-11-06 20:00:21
【问题描述】:

我需要在左侧使用 MKPinAnnotationViewUIButton 使用如下图像:

我尝试编写此代码,但它不起作用:

 let navBtn = UIButton(type: .Custom)
    navBtn.setImage(UIImage(named: "TakeMe.png"), forState: .Selected)
    navBtn.tag = artwork.id
    navBtn.addTarget(self, action: "wazeNav:" , forControlEvents: UIControlEvents.TouchUpInside)

    pinView?.rightCalloutAccessoryView = btn
    pinView?.leftCalloutAccessoryView = navBtn

请帮助我如何做到这一点?

【问题讨论】:

  • 你有什么错误吗?
  • 不只是看不到按钮
  • 你是在 viewForAnnoation 中调用这个吗?
  • 我不这么认为,我该怎么做?

标签: ios swift uibutton mkpinannotationview


【解决方案1】:

请参考下面的代码

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

    let annotationView = AttractionAnnotationView(annotation: annotation, reuseIdentifier: "Attraction")

    let button : UIButton = UIButton(type: UIButtonType.InfoDark)

    button.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    button.addTarget(self, action: "funcRightAcessoryButton:", forControlEvents: UIControlEvents.TouchUpInside)
    annotationView.rightCalloutAccessoryView = button
    annotationView.canShowCallout = true

    let image = UIImage(named: "left_icon")
    let buttonLeft : UIButton = UIButton(type: UIButtonType.Custom)
    buttonLeft.setImage(image, forState: UIControlState.Normal)

    buttonLeft.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
    buttonLeft.addTarget(self, action: "funcLeftAcessoryButton:", forControlEvents: UIControlEvents.TouchUpInside)

    annotationView.leftCalloutAccessoryView = buttonLeft

    return annotationView
}

希望对你有帮助

【讨论】:

  • Workkkkkkk 坦克你是王者!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多