title: Customizing callout view for MKAnnotation in Swift
body: <p>I need a completely customizable callout for for <code>MKAnnotation</code>. I have subclassed <code>MKAnnotationView</code> and an <code>UIView</code>. My code:</p>
  
  <pre><code>class CustomAnnotationView: MKAnnotationView {
  
      class var reuseIdentifier:String {
          return "CustomAnnotationView"
      }
  
      private var calloutView:MapPinCallOut?
      private var hitOutside:Bool = true
  
      var preventDeselection:Bool {
          return !hitOutside
      }
  
  
      convenience init(annotation:MKAnnotation!) {
          self.init(annotation: annotation, reuseIdentifier: CustomAnnotationView.reuseIdentifier)
          //false?
          canShowCallout = true;
      }
  
      override func setSelected(selected: Bool, animated: Bool) {
          let calloutViewAdded = calloutView?.superview != nil
  
          if (selected || !selected &amp;&amp; hitOutside) {
              super.setSelected(selected, animated: animated)
          }
  
          self.superview?.bringSubviewToFront(self)
  
          if (calloutView == nil) {
              calloutView = MapPinCallOut()
          }
  
          if (self.selected &amp;&amp; !calloutViewAdded) {
              addSubview(calloutView!)
          }
  
          if (!self.selected) {
              calloutView?.removeFromSuperview()
          }
      }
  
      override func hitTest(point: CGPoint, withEvent event: UIEvent?) -&gt; UIView? {
          var hitView = super.hitTest(point, withEvent: event)
  
          if let callout = calloutView {
  
              if (hitView == nil &amp;&amp; self.selected) {
                  hitView = callout.hitTest(point, withEvent: event)
              }
          }
  
          hitOutside = hitView == nil
          return hitView;
      }
  }
  </code></pre>
  
  <p>Here is my <code>UIView</code>:</p>
  
  <pre><code>class MapPinCallOut: UIView {
  override func hitTest(var point: CGPoint, withEvent event: UIEvent?) -&gt; UIView? {
      let viewPoint = superview?.convertPoint(point, toView: self) ?? point
  
      let isInsideView = pointInside(viewPoint, withEvent: event)
  
      var view = super.hitTest(viewPoint, withEvent: event)
  
      return view
  }
  
  override func pointInside(point: CGPoint, withEvent event: UIEvent?) -&gt; Bool {
      return CGRectContainsPoint(bounds, point)
  }
  }
  </code></pre>
  
  <p>I have also created a nib file and changed the file's owner to "<code>MapPinCallOut</code>"
  Then in my <code>viewDidLoad</code> of my <code>viewController</code>, I am adding a pin like this:</p>
  
  <pre><code>let location = CLLocationCoordinate2D(
          latitude: 25.0336,
          longitude: 121.5650
      )
      let span = MKCoordinateSpanMake(0.05, 0.05)
      let region = MKCoordinateRegion(center: location, span: span)
      mapView.setRegion(region, animated: true)
      let string = "title"
  
    let annotation = CustomAnnotation(location: location, title: string, subtitle: string)
  
      mapView.addAnnotation(annotation)
  </code></pre>
  
  <p>and then in my viewForAnnotation function:</p>
  
  <pre><code>    var pinView: CustomAnnotationView = CustomAnnotationView()
      pinView.annotation = annotation
      pinView.canShowCallout = true
      return pinView
  </code></pre>
  
  <p>After all that, I ran my code and when I select the annotation, the callout does appear but it is still the standard annotation. What am I missing to create my own custom callout view? </p>