MKPinAnnotationView中的UIButton与图像

时间:2015-11-06 20:00:22

标签: ios swift uibutton mkpinannotationview

我需要在MKPinAnnotationView左侧UIButton做一个像这样的图像: enter image description here

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

 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

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

1 个答案:

答案 0 :(得分: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
}

希望有所帮助