ios - 缩小时注释消失

标签 ios swift xcode mkmapview

我有一个 map View ,我在上面放置了注释,但是当我缩小时它们就会消失。我看过其他教程,这些教程表明您必须将显示优先级设置为 .required,但这对我来说仍然不起作用。

这是我的代码

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

        guard let _annotation = annotation as? MyAnnotation else {return nil}
        let identifier = "Annotation"
        var view: MKMarkerAnnotationView

        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            dequeuedView.titleVisibility = .visible
            dequeuedView.displayPriority = .required
            view = dequeuedView
        }
        else
        {
           view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            view.displayPriority = .required
            view.titleVisibility = .visible
        }

        return nil
     }
  }

最佳答案

如果您返回 nil 作为注释 View ,那么它们的显示事实令人难以置信。正如之前提到的this answer解决了大部分问题(this answer的评论也有效)。

如果您正在为注释创建 View ,则应在此方法结束时返回它。为了避免将蓝点替换为当前位置的注释(顺便说一句,此行为似乎是一个错误),请在委托(delegate)方法的开头添加此内容:

guard annotation !== mapView.userLocation else {
            return nil
        }

祝你好运!

关于ios - 缩小时注释消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58366391/

相关文章:

iphone - MapQuest ios API无法请求路线

swift - 如何使用 UserDefaults SwiftUI?

ios - 如何在底部的每个屏幕中显示 View

ios - 检查 gdata 访问 token 的有效性

ios - ip camera feed 到 IOS 应用程序如何允许全局连接?

ios - Swift:在标签背景中制作图像

ios - 为什么顶部布局指南在我的 iMessage 扩展中移动

swift - xcodeproj 1.3.1 出现 Cocoapods 错误

ios - 图像未出现在 Today Extension Widget 中

ios - 在标签栏中推送 View Controller