Custom MKAnnotation button

这一生的挚爱 提交于 2019-12-09 22:19:33

问题


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

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


回答1:


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


来源:https://stackoverflow.com/questions/48722126/custom-mkannotation-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!