2

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

1 回答 1

7

pinColor在 iOS 9 中已弃用,请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 也会通过警告告知您这一点,供您参考。

于 2015-09-28T03:46:33.437 回答