I'm forced to add an image to MKAnnotationView via subview because apparently I cannot have the image's masksToBounds property set to true without having to set canShowCallout to false. Here's the question that covers this: Swift - setting rounded corners to annotation view image I'm not sure what to configure in order to allow the user to tap the center of the image in order to make the callout bubble appear. I've already messed around with centerOffset, calloutOffset, and anchor point. Here's my code:
extension MapViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if let annotation = annotation as? Food {
if annotation == mapView.userLocation {
return nil
}
let identifier = "pin"
var view: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
{
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
let imageView = UIImageView(frame: CGRectMake(0, 0, 45, 45))
imageView.image = UIImage(named:"picture")
imageView.layer.cornerRadius = imageView.layer.frame.size.width / 2
imageView.layer.borderWidth = 1.5
imageView.layer.borderColor = UIColor(red: 230/255, green: 39/255, blue: 39/255, alpha: 1).CGColor
imageView.layer.masksToBounds = true
view.addSubview(imageView)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: 16, y: 16)
view.layer.anchorPoint = CGPointMake(16 , 16)
view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
}
return view
}
return nil
}
}