【问题标题】:Setting custom image - MKPinAnnotationView - Swift 3设置自定义图像 - MKPinAnnotationView - Swift 3
【发布时间】:2016-11-03 10:39:08
【问题描述】:

我正在使用 MapKit,我目前正在尝试在我的引脚上设置自定义引脚注释图像。但是,我很难弄清楚为什么它不起作用。所有其他代码都运行良好,当我在设置后尝试打印图像的框架时,它确实显示了我的“pinImage”的正确尺寸,因此它似乎能够在属性上设置图像。

此外,代理设置正确,这是通过在默认引脚上设置自定义颜色来验证的。

我也尝试过使用“pinImage.png”,但没有成功。而且由于 MKPinAnnotationView 是 MKAnnotationView 的子类,我认为这应该是问题的原因没有问题,并且可以肯定的是,我也尝试使用 MKAnnotationView,但没有运气。

这是我的代码:

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

    if let annotation = annotation as? Pin {
        let identifier = LocalConstants.pinIdentifier
        var view: MKPinAnnotationView
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        } else {

            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            let detailButton = UIButton(type: .detailDisclosure) as UIView
            view.rightCalloutAccessoryView = detailButton
            //view.pinTintColor = Util.Colors.pluppPurple
        }
        view.image = UIImage(named: "pinImage")
        return view
    }

提前致谢!

【问题讨论】:

    标签: swift mapkit swift3 mkannotationview mkpinannotationview


    【解决方案1】:

    答案确实是使用 MKAnnotationView 而不是 MKPinAnnotationView。我不知道我昨天尝试时搞砸了什么。最终工作副本如下,以供将来参考:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        if let annotation = annotation as? Pin {
            let identifier = LocalConstants.pinIdentifier
            var view: MKAnnotationView
    
            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
                dequeuedView.annotation = annotation
                view = dequeuedView
            } else {
                view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.canShowCallout = true
                view.calloutOffset = CGPoint(x: -5, y: 5)
                view.tintColor = Util.Colors.pluppGrey
    
                let detailButton = UIButton(type: .detailDisclosure) as UIView
                view.rightCalloutAccessoryView = detailButton
            }
            view.image = UIImage(named: LocalConstants.pluppPin)
            return view
        }
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多