带有图像的 MKPinAnnotationView 内的 UIButton

Posted

技术标签:

【中文标题】带有图像的 MKPinAnnotationView 内的 UIButton【英文标题】:UIButton inside MKPinAnnotationView with image 【发布时间】: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 中调用这个吗? 我不这么认为,我该怎么做? 【参考方案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 坦克你是王者!

以上是关于带有图像的 MKPinAnnotationView 内的 UIButton的主要内容,如果未能解决你的问题,请参考以下文章

如何在常规 UIView 之上使用 MKPinAnnotationView(而不是在地图上)

MKPinAnnotationView 无法设置自定义图像

MKPinAnnotationView 没有设置自定义图像

自定义 MKPinAnnotationView 颜色

如何从 iphone 模拟器中获取徽章图标图像?

MKPinAnnotationView 不动