如何添加两个或更多的buttonannotationView:MKAnnotationView?

我想有一个MKAnnotationView到我的地图上的引脚(使用MapKit)与下一个属性:

1)图像

2)详细button

3)另一个细节button

4)另一个细节button?

我已经做了实际添加图像和细节button与下一个代码:

annotationView.detailCalloutAccessoryView = snapshotView // snapshot view is a custom view // with image and its constraints let detailsButton = UIButton(type: .detailDisclosure) annotationView.rightCalloutAccessoryView = detailsButton 

所以,问题是如何添加多个button到MKAnnotationView

因为我见过的所有教程只是“如何添加细节button”。

你可以使用detailCalloutAccessoryViewMKAnnotationView来实现这一点。

示例如何使用UIStackView扩展UIStackView

 extension MKAnnotationView { func conteiner(arrangedSubviews: [UIView]) { let stackView = UIStackView(arrangedSubviews: arrangedSubviews) stackView.axis = .vertical stackView.distribution = .fillEqually stackView.alignment = .fill stackView.spacing = 5 stackView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin, .flexibleWidth, .flexibleHeight] stackView.translatesAutoresizingMaskIntoConstraints = false self.detailCalloutAccessoryView = stackView } } 

以及如何实现这一点:

 extension ViewController: MKMapViewDelegate { func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let annotationIdentifier = "AnnotationIdentifier" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) annotationView!.canShowCallout = true annotationView!.conteiner(arrangedSubviews: [UIButton(type: .detailDisclosure), UIButton(type: .detailDisclosure), UIButton(type: .detailDisclosure)]) } else { annotationView!.annotation = annotation } return annotationView } } 

结果: 在这里输入图像说明

我已经做了

从左侧和右侧的文档calloutAccessoryView是低宽度和高度。 所以,我们可以只添加button和图像detailCalloutAccessoryView。

这是我的代码。 它正在工作。 我没有做过评论。 因为理解更清楚。

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { if !(view.annotation! is MKUserLocation) { let customPin = view.annotation as! CustomPin self.spotDetailsForSendToPostsStripController = customPin.spotDetailsItem // its for sending to another controller. configureDetailView(annotationView: view, spotPin: customPin.spotDetailsItem) } } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } if !(annotation is CustomPin) { return nil } let identifier = "CustomPin" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView?.canShowCallout = true } else { annotationView!.annotation = annotation } return annotationView } func configureDetailView(annotationView: MKAnnotationView, spotPin: SpotDetailsItem) { let width = 250 let height = 250 let snapshotView = UIView() let views = ["snapshotView": snapshotView] snapshotView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[snapshotView(250)]", options: [], metrics: nil, views: views)) snapshotView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[snapshotView(250)]", options: [], metrics: nil, views: views)) let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height - 40)) // configure button1 let button1 = UIButton(frame: CGRect(x: 0, y: height - 35, width: width / 2 - 5, height: 35)) button1.setTitle("Info", for: .normal) button1.backgroundColor = UIColor.darkGray button1.layer.cornerRadius = 5 button1.layer.borderWidth = 1 button1.layer.borderColor = UIColor.black.cgColor button1.addTarget(self, action: #selector(MainFormController.goToInfo), for: .touchDown) // configure button2 let button2 = UIButton(frame: CGRect(x: width / 2 + 5, y: height - 35, width: width / 2, height: 35)) button2.setTitle("Posts", for: .normal) button2.backgroundColor = UIColor.darkGray button2.layer.cornerRadius = 5 button2.layer.borderWidth = 1 button2.layer.borderColor = UIColor.black.cgColor button2.addTarget(self, action: #selector(MainFormController.goToPosts), for: .touchDown) // configure image let image = UIImage(contentsOfFile: "plus-512.gif") imageView.image = image // implement your own logic imageView.layer.cornerRadius = imageView.frame.size.height / 10 imageView.layer.masksToBounds = true imageView.layer.borderWidth = 0 imageView.contentMode = UIViewContentMode.scaleAspectFill // adding it to view snapshotView.addSubview(imageView) snapshotView.addSubview(button1) snapshotView.addSubview(button2) annotationView.detailCalloutAccessoryView = snapshotView } func goToPosts() { print("go to posts") // your implementation(segues and etc) } func goToInfo() { print("go to info") // your implementation(segues and etc) } 

CustomPin:

 class CustomPin: MKPointAnnotation { var spotDetailsItem: SpotDetailsItem! // its my info of this place } 

奇迹般有效

结果