更改颜色图钉iOS 9 Mapkit

时间:2015-09-28 03:39:32

标签: ios swift2 ios9 mapkitannotation

我不知道如何在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.
    }
}

1 个答案:

答案 0 :(得分:6)

在iOS 9中不推荐使用

pinColor,而是使用pinTintColor

示例:

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

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

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

如果你的最小目标是iOS 9,正如你在这里特别提出的那样,上面的内容将是多余的 - Xcode会通过警告告诉你,以及你的信息。