作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一张 map ,其中填充了餐车列表,并且我想在选择注释
时实现如下所示的自定义标注。目前,我已经使用标题和副标题实现了标准注释标注。
我已经阅读了一些内容,猜测它需要一个自定义的 .xib 文件,但我对此一无所知,因为我最近才开始为 iOS 进行开发。我将如何创建此自定义弹出窗口,以及如何使其在单击时执行导航操作?
谢谢!
最佳答案
首先要更改您需要使用的 pin 图像 viewForAnnotation
方法MKMapViewDelegate
并设置image
MKAnnotationView
的属性(property)还可以显示您自己的 View 集canShowCallout
属性至false
.
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation.isKindOfClass(MKPointAnnotation.classForCoder())) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
else {
annotationView!.annotation = annotation
}
annotationView!.image = UIImage(named: "Marker")
annotationView!.canShowCallout = false
return annotationView
}
return nil
}
现在要显示您自己的 View ,请使用 didSelectAnnotationView
方法MKMapViewDelegate
.
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
mapView.deselectAnnotation(view.annotation, animated: true)
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("AnnotationInfoViewController") as! AnnotationInfoViewController
//If you want to show view from XIB file you can create viewController instance like this
//let viewController = UIViewController()
//let myView = NSBundle.mainBundle().loadNibNamed("AnnotationInfoView", owner: self, options: nil)[0] as! AnnotationInfoView
//viewController.view = myView
//Pass the information that you want to show
viewController.data = passdata
//Set the viewController for popoverPresentationController
viewController.modalPresentationStyle = .Popover
viewController.preferredContentSize = CGSize(width: viewController.view.frame.size.width, height: 80)
viewController.popoverPresentationController!.delegate = self;
viewController.popoverPresentationController!.permittedArrowDirections = [.Up , .Down]
viewController.popoverPresentationController!.sourceView = view.superview!
viewController.popoverPresentationController!.sourceRect = view.frame
self.presentViewController(viewController, animated: true, completion: nil)
}
现在显示弹出 View 工具 adaptivePresentationStyleForPresentationController
方法UIPopoverPresentationControllerDelegate
.
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None;
}
注意:现在,如果您想处理此弹出窗口的点击事件,您可以使用 UITapGestureRecognizer
.
关于ios - 如何创建自定义引脚注释 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38734882/
我是一名优秀的程序员,十分优秀!