在 swift 中为 callOut 视图添加标题

Add a title to callOut view in swift

我创建了下面的视图,我想在 callOut 中添加标题,但我似乎无法弄清楚在哪里以及如何做。我想将它添加到注释中,在 else if annotation.isKindOfClass(FBAnnotation) 语句中

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 {

}

您似乎将 canShowCallout 设置为 false。如果你想要标注,这应该是正确的。

pinView?.canShowCallout = false

您还必须在 MKAnnotation 中设置标题 属性 才能在标注中显示。

MKAnnotation 协议有一个标题,但它是可选的,因此您必须确保它已在您的自定义注释中实现

class FBAnnotation : NSObject, MKAnnotation {

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

class FBAnnotation : NSObject {

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

}

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