简体   繁体   English

更改颜色别针 iOS 9 Mapkit

[英]Change color pin iOS 9 Mapkit

I don't know how to change the code for the pin color in iOS 9 (because lately Apple changed the code for it), and I'm still new in Swift.我不知道如何在 iOS 9 中更改引脚颜色的代码(因为最近 Apple 更改了它的代码),而且我还是 Swift 的新手。 So, I don't know how to integrate pinTintColor in my code now.所以,我现在不知道如何在我的代码中集成pinTintColor

Please find below my code:请在下面找到我的代码:

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 is deprecated in iOS 9, use pinTintColor instead. pinColor在 iOS 9 中已弃用,请改用pinTintColor

Example:例子:

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

Although the OP specifically asks for iOS 9, the following could ensure that the "non-deprecated" method prior to iOS 9 could be called:尽管 OP 特别要求使用 iOS 9,但以下内容可以确保可以调用 iOS 9 之前的“非弃用”方法:

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

If your minimal target is iOS 9 as you specifically ask here, the above would then be redundant - Xcode would let you know that with a warning, as well, for your information.如果您的最小目标是 iOS 9,那么上面的内容将是多余的 - Xcode 也会通过警告让您知道这一点,以供您参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM