iosswiftuibuttonmkpinannotationview

UIButton inside MKPinAnnotationView with image


i need to do MKPinAnnotationView with UIButton to the left side with a image like this: enter image description here

i try to write this code but it's not work:

 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

plz help me how i can do this?


Solution

  • Please refer the code below

    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
    }
    

    Hope it helps