2016-06-12 9 views
2

Ich habe eine mapView mit Anmerkungen, die Titel und Untertitel anzeigen. Die Untertitel sind manchmal länger als die Breite der Anmerkung, also frage ich mich, ob ich sie mehrzeilig machen kann? Es ist so weit wie folgt codiert:Längere Untertitel in MapView-Anmerkungen (swift)

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

Dann habe ich ein paar Optionen in

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

, die hier nicht relevant sind.

Ist es möglich, eine benutzerdefinierte Anmerkungsansicht zu erstellen? Ich habe ein paar Dinge ausprobiert, aber nichts hat funktioniert. Der nächste, den ich bekommen kann, ist das Hinzufügen einer Schaltfläche, um den längeren Untertitel separat anzuzeigen, aber ich würde es lieber in der Anmerkung haben.

Ist es möglich?

+0

plz siehe diesen Link http://stackoverflow.com/ Fragen/5831382/How-to-Display-2-Zeilen-von-Text-für-Untertitel-von-mkannotation-und-das Bild-Bild –

+0

für schnelle http://StackOverflow.com/questions/37446219/swift- 2-multiline-mkpointannotation –

Antwort

8

ich es herausgefunden, fügte ich ein Etikett in viewForAnnotation und es funktionierte nur

¯ \ _ (ツ) _/¯

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

Gute Antwort, aber "detailCalloutAccessoryView" ist seit 9.0 verfügbar –