更改颜色pin iOS 9 Mapkit

我不知道如何改变在iOS 9的针颜色的代码(因为最近苹果改变了它的代码),我还是在Swift新。 所以,我不知道如何将pinTintColor集成到我的代码中。

请在下面find我的代码:

 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() 

尽pipeOP特别要求iOS 9,但以下内容可以确保可以调用iOS 9之前的“不推荐”方法:

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

如果你的最小目标是iOS 9,就像你在这里特别提出的那样,那么上面的内容就会是多余的–Xcode会让你知道,同时也有一个警告信息。