更改颜色图钉iOS 9 Mapkit

2 ios mapkitannotation ios9 swift2

我不知道如何在iOS 9中更改引脚颜色的代码(因为最近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.
    }
}
Run Code Online (Sandbox Code Playgroud)

Unh*_*lig 7

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

例:

let annotationView = MKPinAnnotationView()
annotationView.pinTintColor = UIColor.purpleColor()
Run Code Online (Sandbox Code Playgroud)

虽然OP专门要求iOS 9,但以下内容可以确保可以调用iOS 9之前的"非弃用"方法:

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

如果你的最小目标是iOS 9,正如你在这里特别要求的那样,上面的内容将是多余的 - Xcode会通过警告通知您,以获取您的信息.

  • @rmaddy但他问iOS 9的问题 - "我不知道如何更改iOS 9中针脚颜色的代码"_. (2认同)