【问题标题】:Longer subtitles in MapView annotations (swift)MapView 注释中较长的字幕(swift)
【发布时间】:2016-06-12 09:11:48
【问题描述】:

我有一个带有显示标题和副标题的注释的 mapView。字幕有时比注释的宽度长,所以我想知道是否可以将它们设为多行? 到目前为止,它的编码是这样的:

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

然后我设置了一些选项

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

这里不相关。

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

有可能吗?

【问题讨论】:

标签: ios swift mapkit mkannotation mkpointannotation


【解决方案1】:

我想通了,我在 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
}

【讨论】:

  • 不错的答案,但“detailCalloutAccessoryView”自 9.0 起可用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 2017-06-11
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多