ios - 如何创建自定义引脚注释 View

标签 ios swift mapkit xib mkannotationview

我有一张 map ,其中填充了餐车列表,并且我想在选择注释时实现如下所示的自定义标注。目前,我已经使用标题和副标题实现了标准注释标注。

我已经阅读了一些内容,猜测它需要一个自定义的 .xib 文件,但我对此一无所知,因为我最近才开始为 iOS 进行开发。我将如何创建此自定义弹出窗口,以及如何使其在单击时执行导航操作?

谢谢!

enter image description here

最佳答案

首先要更改您需要使用的 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/

相关文章:

ios - 如何在 iOS Swift 中剪切 CALayer 的一部分?

ios - 如何将 MKMapItem 转换为二进制数据存储在 Core Data 中?

iphone - 创建一个自定义 View 来替换 UINavigationBar

ios - 屏幕特定区域的 UIButtons 会延迟 Touch Down 事件

ios - 删除路径存储为文本且实际文件保存在磁盘上的核心数据项的最佳实践?

swift - 在不使用关联类型的情况下,您能否将协议(protocol)中的变量限制为原始类型为 String 的 RawRepresentables?

ios - CollectionViewCell 的容器 View 未填充单元格

ios - 沿其中心旋转 SKShapeNode

ios - Mapkit 上的动画标记

ios - 自定义 MKAnnotationView 间歇性识别触摸