0

Ich benutze die benutzerdefinierte MKAnnotationView. Ich verwende die autolayouts, die gut funktionieren, aber die Position (Rahmen) der Anmerkungsansicht ist nicht geeignet.Legen Sie das Zentrum der benutzerdefinierten MKAnnotationView

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 
{ 
    if view.annotation is MKUserLocation 
    { 
     return 
    } 

    let starbucksAnnotation = view.annotation as! LiquorLocation 
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil) 
    let calloutView = views?[0] as! CustomCalloutView 
    calloutView.labelLiquorShopName.text = starbucksAnnotation.title 
    calloutView.labelLiquorShopAddress.text = starbucksAnnotation.address 
    calloutView.labelLiquorShopAwayDistance.text = starbucksAnnotation.locationKmAway 
    calloutView.imageViewLiquorShop.image = starbucksAnnotation.liquorShopIcon 


    calloutView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(calloutView) 
    mapView.setCenter((view.annotation?.coordinate)!, animated: true) 
} 

enter image description here

Was mache ich falsch? Ich möchte die Anmerkungsansicht oben auf dem Pin zentrieren.

Antwort

0

Autolayouts wieder meinen Tag retten. Ich füge die Layouteinschränkung zwischen meiner Calloutansicht und Ansicht hinzu (MKAnnotationView). Ich schreibe unter Linien view.addSubview(calloutView) in didSelect Methode.

let horizontalConstraint = NSLayoutConstraint(item: calloutView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0) 
     let verticalConstraint = NSLayoutConstraint(item: calloutView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0) 
     let widthConstraint = NSLayoutConstraint(item: calloutView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: calloutView.frame.size.width) 
     view.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint]) 
Verwandte Themen