MapKit iOS 9 detailCalloutAccessoryView用法 [英] MapKit iOS 9 detailCalloutAccessoryView usage

查看:92
本文介绍了MapKit iOS 9 detailCalloutAccessoryView用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观看 WWDC视频206 之后,我认为这将是一个将细节标注视图添加到mapView注释视图的琐碎任务.

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 wrong.

设置了我的图钉视图

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
}

我只有这个

我知道该视图正在运行,因为如果使用leftCalloutAccessoryView进行尝试,则会得到

I know the view is working, because if I try it with the leftCalloutAccessoryView I get

我一定很想念东西.请注意,如果我只是将图像添加到detailCalloutAccessoryView之类的

I must be missing something. Note, if I just add an image to the detailCalloutAccessoryView like

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

图像在那里,尺寸正确等

The image is there, size correctly etc

我只是想不通如何放置自己的自定义视图.

I just cannot figure out how to put in my own custom view.

谢谢

推荐答案

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

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

添加intrinsicContentSize也可以.

我进行了一些测试,发现当您将视图设置为detailCalloutAccessoryView时,MapKit会自动将translatesAutoresizingMaskIntoConstraints设置为false.

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

在WWDC 2015会话中"MapKit的新增功能" 有人说,支持自动布局".我认为Apple确实意味着,您必须使用自动布局?!

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?!

这篇关于MapKit iOS 9 detailCalloutAccessoryView用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆