Peter Pik
Peter Pik

Reputation: 11193

Add a title to callOut view in swift

I've created below view, where I want to add a title in the callOut, however I can't seem to figure out where and how to do it. I want to add it to the annotations, which is in the else if annotation.isKindOfClass(FBAnnotation) statement

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    var reuseId = ""

    if annotation.isKindOfClass(FBAnnotationCluster) {
        var reuseId = "Annonation"
        reuseId = "Cluster"
        var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId)

        return clusterView

    } else if annotation.isKindOfClass(FBAnnotation) {

        let singleAnnotation = annotation as! FBAnnotation
        reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: reuseId) as FBSingleClusterView
        pinView!.image = UIImage(named: singleAnnotation.imageString)


        pinView?.canShowCallout = false
        let imagePinView = UIImageView(frame: CGRectMake(7.5, 7.5, pinView!.image!.size.width-15, pinView!.image!.size.width-15))
        imagePinView.clipsToBounds = true
        imagePinView.layer.cornerRadius = (pinView!.image!.size.width-15)/2
        imagePinView.contentMode = UIViewContentMode.ScaleAspectFill
        imagePinView.image = UIImage(data: singleAnnotation.logoImage)
        pinView?.addSubview(imagePinView)




        return pinView
    } else {
        return nil
    }

}

FBAnnotation

class FBAnnotation : NSObject {

    var coordinate = CLLocationCoordinate2D()
    var imageString = ""
    var logoImage = NSData()

}

extension FBAnnotation : MKAnnotation {

}

Upvotes: 1

Views: 485

Answers (1)

Moriya
Moriya

Reputation: 7906

Seems you set the canShowCallout to false. This should probably be true if you want a callout.

pinView?.canShowCallout = false

you also have to set the title property in MKAnnotation for it to show in the callout.

MKAnnotation protocol has a title but it's optional so you have to make sure it's implemented in your custom annotation

class FBAnnotation : NSObject, MKAnnotation {

    var coordinate = CLLocationCoordinate2D()
    var imageString = ""
    var logoImage = NSData()
    var title: String?
    var subtitle: String?
} 

or

class FBAnnotation : NSObject {

    var coordinate = CLLocationCoordinate2D()
    var imageString = ""
    var logoImage = NSData()

}

extension FBAnnotation : MKAnnotation {
    var title: String?
    var subtitle: String?    
}

Upvotes: 2

Related Questions