带有图像的 MKPinAnnotationView 内的 UIButton
UIButton inside MKPinAnnotationView with image
我需要在左侧使用 UIButton
做 MKPinAnnotationView
,图像如下:
我尝试编写这段代码,但它不起作用:
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
请帮我看看我该怎么做?
请参考以下代码
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
}
希望对您有所帮助
我需要在左侧使用 UIButton
做 MKPinAnnotationView
,图像如下:
我尝试编写这段代码,但它不起作用:
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
请帮我看看我该怎么做?
请参考以下代码
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
}
希望对您有所帮助