在自定义MKPinAnnotationView中显示注释标题和子标题 [英] Show Annotation Title and SubTitle in Custom MKPinAnnotationView

查看:340
本文介绍了在自定义MKPinAnnotationView中显示注释标题和子标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序内使用MKPinAnnotationView.

I am using MKPinAnnotationView inside my App.

我将MapView对象设置为委托,并使用此代码来自定义我的AnnotationView

I am setting MapView object as delegate, and using this code for customising my AnnotationView

   func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
           // pinView!.canShowCallout = true
            pinView!.image = UIImage(named:"store.jpg")
            pinView!.animatesDrop = true
            pinView!.pinTintColor = UIColor.darkGrayColor()
         }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }

我正在获得所需的自定义AnnotationView.但是,我缺少MKPointAnnotationtitlesubtitle功能.

I am getting custom AnnotationView as I required.However, I am missing the features of title and subtitle of MKPointAnnotation.

我希望看到灰色点的标题和副标题.

I wish to see title and subtitle for the grey dots.

推荐答案

我覆盖了一个func

I was overriding one func

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        mapView.deselectAnnotation(view.annotation, animated: true)
    }

我注释掉了此功能,并获得了标题和字幕.

I commented this function out and got the titles and subtitles.

更新代码

/*
 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
 mapView.deselectAnnotation(view.annotation, animated: true)
 }
 */
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView.canShowCallout = true
    pinView.animatesDrop = true
    pinView.pinTintColor = UIColor.darkGrayColor()
    pinView.draggable = true
    pinView.accessibilityLabel = "hello"
    let btn = UIButton(type: .DetailDisclosure)
    pinView.rightCalloutAccessoryView = btn
    return pinView
}

这篇关于在自定义MKPinAnnotationView中显示注释标题和子标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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