Custom pin image in annotationView in iOS

前端 未结 3 1951
一个人的身影
一个人的身影 2020-12-07 21:14

I\'m trying to change from Swift 1.2 to Swift 2.0 and I\'m at the end of the changes. Currently I\'m making changes in the MapViewController, and there isn\'t any error or w

3条回答
  •  醉酒成梦
    2020-12-07 21:50

    Here is the answer:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    
        let identifier = "MyPin"
    
        if annotation.isKindOfClass(MKUserLocation) {
            return nil
        }
    
        let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
    
        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "custom_pin.png")
            annotationView.rightCalloutAccessoryView = detailButton
        }
        else {
            annotationView.annotation = annotation
        }
    
        return annotationView
    }
    

    Regards

提交回复
热议问题