Change color pin iOS 9 Mapkit
Asked Answered
T

1

2

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. So, I don't know how to integrate pinTintColor in my code now.

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.
    }
}
Turnstile answered 28/9, 2015 at 3:39 Comment(0)
Z
7

pinColor is deprecated in iOS 9, use pinTintColor instead.

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:

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.

Zaratite answered 28/9, 2015 at 3:46 Comment(3)
If the app still needs to support iOS 8 then this change should not be done.Linettelineup
@Linettelineup But he is asking the question in terms of iOS 9 - "I don't know how to change the code for the pin color in iOS 9".Zaratite
The OP may be a bit confused here and not realize they should probably support iOS 8 still which means there is nothing to change. I'm simply pointing out a possibility, just in case.Linettelineup

© 2022 - 2024 — McMap. All rights reserved.