Jonas0000
Jonas0000

Reputation: 1113

Custom MKAnnotation button

I'd like to generate a custom annotation using MKAnnotation.

imgA

annotationView?.image = UIImage(named: "annotation)") // this is to set the annotation image
annotationView?.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "mySymbol")) // this is to add a icon left to the annotation UI

The few lines above are working quiet well but ... now I'd like to add a custom button to the Annotation like in the image above.


Using the default one is working so far :

annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

... but using a custom one is not (Just not showing a UI element, there is still NOTHING):

let myButton = UIButton(type: .custom)
myButton.setImage(UIImage(named: "myButton"), for: .normal)
annotationView?.rightCalloutAccessoryView = myButton

I have no clue how to go on... I hope somebody is able to fix this small problem.

Thanks in advance, jonas

Upvotes: 2

Views: 332

Answers (1)

Jonas0000
Jonas0000

Reputation: 1113

I'm the question asker :-)

I found a solution on my own without creating a complete new view - like @Sh_Khan mentioned - because this is still not necessary.

This is what I came up with:

let button = UIButton()
button.setImage(UIImage(named :"myIcon")?.withRenderingMode(.alwaysTemplate), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
annotationView?.rightCalloutAccessoryView = button

Upvotes: 3

Related Questions