Longer subtitles in MapView annotations (swift)

前端 未结 1 625
盖世英雄少女心
盖世英雄少女心 2021-01-15 13:20

I have a mapView with annotations displaying titles and subtitles. The subtitles are sometimes longer than the width of the annotation, so I am wondering if i can make them

1条回答
  •  北海茫月
    2021-01-15 14:09

    I figured it out, I added a label in viewForAnnotation and it just worked

    ¯\_(ツ)_/¯

    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
    }
    

    0 讨论(0)
提交回复
热议问题