MapKit iOS 9 detailCalloutAccessoryView用法

Dog*_*fee 18 mapkit mkannotationview ios

在观看WWDC视频206后,我认为将详细的标注视图添加到mapView注释视图是一项微不足道的任务.

所以,我认为我做错了什么.

设置了引脚视图

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

    let view:MKAnnotationView!

    if let dequed = routeMapView.dequeueReusableAnnotationViewWithIdentifier("pin") {
        view = dequed
    }
    else {
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    }

    let x = UIView(frame: CGRectMake(0, 0, 200, 200))
    x.backgroundColor = UIColor.redColor()

    // shows the red
    //view.leftCalloutAccessoryView = x

    // working as no subtitle - but no red view
    view.detailCalloutAccessoryView = x

    view.canShowCallout = true
    return view
}
Run Code Online (Sandbox Code Playgroud)

我只能得到这个

在此输入图像描述

我知道这个观点正在发挥作用,因为如果我试着用它leftCalloutAccessoryView来做 在此输入图像描述

我肯定错过了什么.请注意,如果我只是添加图像detailCalloutAccessoryView

view.detailCalloutAccessoryView = UIImage(named:"YourImageName")
Run Code Online (Sandbox Code Playgroud)

图像在那里,尺寸正确等

我只是无法弄清楚如何放入我自己的自定义视图.

谢谢

Kla*_*aas 20

您必须为视图的宽度和高度添加一些约束:

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!
}
Run Code Online (Sandbox Code Playgroud)

1972年慕尼黑

添加intrinsicContentSize也有效.

我做了一些测试并发现translatesAutoresizingMaskIntoConstraints,当你设置一个视图时,MapKit会自动设置为false detailCalloutAccessoryView.

在WWDC 2015会议上,"MapKit的新功能"被告知,"支持自动布局".我认为Apple确实意味着你必须使用自动布局?!

  • 你如何摆脱`detailCalloutAccessoryView`周围添加的间距?@Klaas (5认同)

Dog*_*fee 13

1.创建一个UIView并将其添加到故事板中的地图VC

在此输入图像描述

在这里,您可以设置大小,约束,添加按钮,图像等 - 布局您想要的方式.在这种情况下,堆栈视图非常完美.

2.创建并插入Maps VC

从您的自定义视图中按照惯例控制拖动.

@IBOutlet var customDetailView: UIView!
Run Code Online (Sandbox Code Playgroud)

3.为您的引脚设置detailCalloutAccessoryView

例如

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    view.detailCalloutAccessoryView = customDetailView
}
Run Code Online (Sandbox Code Playgroud)

成功

在此输入图像描述

  • 这可能看起来很明显,但它让我抓住了几分钟......试图将UIView拖入故事板中的开放空间是行不通的.你需要将它拖到你希望使用它的UIViewController上方的顶部栏上.就像那个黄色圆圈,红色框等旁边...... (5认同)