更改颜色图钉 iOS 9 Mapkit

Change color pin iOS 9 Mapkit

我不知道如何在 iOS 9 中更改 pin 颜色的代码(因为最近 Apple 更改了它的代码),而且我在 Swift 中还是新手。所以,我现在不知道如何在我的代码中集成 pinTintColor

请在下面找到我的代码:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let annotation = MKPointAnnotation()
        let latitude:CLLocationDegrees = 40.5
        let longitude:CLLocationDegrees = -74.6
        let latDelta:CLLocationDegrees = 150
        let lonDelta:CLLocationDegrees = 150
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        map.setRegion(region, animated: false)

        annotation.coordinate = location
        annotation.title = "Niagara Falls"
        annotation.subtitle = "One day bla bla"
        map.addAnnotation(annotation)
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // simple and inefficient example

        let annotationView = MKPinAnnotationView()

        annotationView.pinColor = .Purple

        return annotationView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

pinColor 在 iOS 9 中已弃用,请改用 pinTintColor

示例:

let annotationView = MKPinAnnotationView()
annotationView.pinTintColor = UIColor.purpleColor()

尽管 OP 特别要求 iOS 9,但以下内容可以确保可以调用 iOS 9 之前的 "non-deprecated" 方法:

if #available(iOS 9, *) {
    annotationView.pinTintColor = UIColor.purpleColor()
} else {
    annotationView.pinColor = .Purple
}

如果您的最低目标是 iOS 9,正如您在此处特别询问的那样,以上内容将是多余的 - Xcode 也会通过警告让您知道,供您参考。