swift2 更改颜色引脚iOS 9 Mapkit

bq9c1y66  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(206)

我不知道如何在iOS9中更改pin颜色的代码(因为苹果最近更改了它的代码),而且我在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.
    }
}
mccptt67

mccptt671#

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
}

如果你的最低目标是iOS9,就像你在这里特别要求的那样,那么上面的内容就是多余的-- Xcode会让你知道这一点,并给出警告,供你参考。

相关问题