【问题标题】:Show Annotation Title and SubTitle in Custom MKPinAnnotationView在自定义 MKPinAnnotationView 中显示注释标题和子标题
【发布时间】:2016-06-22 10:09:27
【问题描述】:

我在我的应用程序中使用MKPinAnnotationView

我将MapView 对象设置为委托,并使用此代码自定义我的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 的功能。

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

【问题讨论】:

    标签: ios swift mkmapview mkannotationview mkpointannotation


    【解决方案1】:

    我正在覆盖一个函数

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

    我把这个函数注释掉了,得到了标题和副标题。

    更新代码

    /*
     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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多