MapKit iOS 9 detailCalloutAccessoryView用法

观看 WWDC video 206后,我认为这将是一个简单的任务,将详细调用视图添加到mapView注释视图.

所以,我假设我做错了事情.

用我的pin视图设置

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,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
}

我只得到这个

我知道这个视图是有效的,因为如果我用leftCalloutAccessoryView来尝试它

我一定是错过了一些东西.注意,如果我只是添加一个图像到detailCalloutAccessoryView像

view.detailCalloutAccessoryView = UIImage(named:"YourImageName")

图像在那里,尺寸正确等

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

谢谢

解决方法

您必须为视图的宽度和高度添加一些约束:
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,constant: 20)
    myView.addConstraint(heightConstraint)

    av!.detailCalloutAccessoryView = myView
    av!.canShowCallout = true

    return av!
}

添加一个intrinsicContentSize也可以.

我做了一些测试,发现,当您将视图设置为detailCalloutAccessoryView时,MapKit将自动设置翻译AutotoresizingMaskIntoConstraints为false.

在WWDC 2015年会议“What’s New in MapKit”中,被告知“支持汽车布局”.我认为苹果真的意味着你必须使用汽车布局?

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...