MapView注释中较长的字幕(swift)

Ric*_*hie 2 mapkit mkannotation ios mkpointannotation swift

我有一个带注释的mapView,显示标题和副标题.字幕有时长于注释的宽度,所以我想知道我是否可以使它们成为多线?它到目前为止编码如此:

func annotate(newCoordinate, title: String, subtitle: String) {
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    annotation.title = title
    annotation.subtitle = subtitle
    self.map.addAnnotation(annotation)    
}
Run Code Online (Sandbox Code Playgroud)

然后我有几个选项

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
Run Code Online (Sandbox Code Playgroud)

这里没有关系.

是否可以制作自定义注释视图?我尝试了几件事,但没有任何效果.我能得到的最接近的是添加一个按钮来分别显示较长的字幕,但我宁愿把它放在注释中.

可能吗?

Ric*_*hie 8

我想通了,我在viewForAnnotation中添加了一个标签,它只是起作用了

¯\ _(ツ)_ /¯

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

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
    }
    else {
        pinView!.annotation = annotation
    }

    //THIS IS THE GOOD BIT
    let subtitleView = UILabel()
    subtitleView.font = subtitleView.font.fontWithSize(12)
    subtitleView.numberOfLines = 0
    subtitleView.text = annotation.subtitle!
    pinView!.detailCalloutAccessoryView = subtitleView


    return pinView
}
Run Code Online (Sandbox Code Playgroud)