gpt4 book ai didi

ios - 为什么我无法自定义 MKAnnotationView?

转载 作者:行者123 更新时间:2023-11-30 12:25:48 25 4
gpt4 key购买 nike

在我的代码中,我使用Apple map 并在 View Controller 中正确设置mapView和自身的委托(delegate)。

在我的 ViewController 中,我还在 for 循环中包含以下代码:

if userOnMapAlready == false {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: otherUserLocation.coordinate.latitude, longitude: otherUserLocation.coordinate.longitude)
annotation.subtitle = otherUserUUID
self.mapView.addAnnotation(annotation)

}

我的 View Controller 中还有以下委托(delegate)函数,并且再次在 View Controller 中设置mapview.delegate = self:

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.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .purple
}
else {
pinView!.annotation = annotation
}

return pinView

}

遗憾的是,当绘制引脚时,它们都是红色的,而不是像 viewForAnnotation 所建议的那样是紫色的。问题是什么?!这是否与reuseId与分配给每个MKPointAnnotation的唯一字幕不匹配有关?

更新:刚刚意识到委托(delegate)函数 viewForAnnotation 从未被调用 - 这可以解释为什么引脚不是紫色的。为什么 viewForAnnotation 不会被调用???

最佳答案

在最新版本的 Swift 中,该属性为:

pinView!.pinTintColor = .purple

或者...

pinView!.pinTintColor = MKPinAnnotationView.purplePinColor()

https://developer.apple.com/reference/mapkit/mkpinannotationview

关于ios - 为什么我无法自定义 MKAnnotationView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44233419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com