MapKit iOS 9 detailCalloutAccessoryView用法
看完WWDCvideo206之后,我认为这将是一个简单的任务,将细节标注视图添加到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 }
我只得到这个
我知道该视图正在工作,因为如果我用我得到的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, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20) myView.addConstraint(heightConstraint) av!.detailCalloutAccessoryView = myView av!.canShowCallout = true return av! }
添加一个intrinsicContentSize
也可以。
我做了一些testing,发现,MapKit自动将translatesAutoresizingMaskIntoConstraints
设置为false,当您将视图设置为detailCalloutAccessoryView
。
在WWDC 2015会议“MapKit新增function”中 ,有人告诉我,“支持自动布局”。 我认为,苹果真的意味着,你必须使用自动布局?!
1.创build一个UIView并将其添加到你的地图VC中的故事板
在这里你可以设置大小,约束,添加button,图像等 – 布局如何你想要的。 在这种情况下,堆栈视图是完美的。
2.创build并发布到您的地图VC
按照惯例,从您的自定义视图中控制拖动。
@IBOutlet var customDetailView: UIView!
3.为您的引脚设置detailCalloutAccessoryView
例如
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { view.detailCalloutAccessoryView = customDetailView }
成功
使用UIImageView
view.detailCalloutAccessoryView = UIImageView(image:UIImage(named:"YourImageName"))