MapKit iOS 9 detailCalloutAccessoryView usage

后端 未结 4 1031
遥遥无期
遥遥无期 2020-12-05 05:39

After watching WWDC video 206 I assumed this would be a trivial task of adding the detail callout view to a mapView annotation view.

So, I assume Im doing something

4条回答
  •  粉色の甜心
    2020-12-05 05:48

    You have to add some constraints for width and height of your view:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        var av = mapView.dequeueReusableAnnotationViewWithIdentifier("id")
        if av == nil {
            av = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
        }
    
        let myView = UIView()
        myView.backgroundColor = .greenColor()
    
        let widthConstraint = NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
        myView.addConstraint(widthConstraint)
    
        let heightConstraint = NSLayoutConstraint(item: myView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20)
        myView.addConstraint(heightConstraint)
    
        av!.detailCalloutAccessoryView = myView
        av!.canShowCallout = true
    
        return av!
    }
    

    Adding an intrinsicContentSize also works.

    I did some testing and found out, that MapKit automagically sets translatesAutoresizingMaskIntoConstraints to false, when you set a view to detailCalloutAccessoryView.

    In the WWDC 2015 session "What's New in MapKit" it was told, that "auto layout is supported". I reckon that Apple really means, that you have to use auto layout?!

提交回复
热议问题