问题描述
我不知道如何在iOS 9中更改引脚颜色的代码(因为最近Apple更改了它的代码),而且我在Swift中仍然是新手。所以,我现在不知道如何在我的代码中集成 pinTintColor
。
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.
}
}
推荐答案
pinColor ,而是使用 pinTintColor
。
示例:
let annotationView = MKPinAnnotationView()
annotationView.pinTintColor = UIColor.purpleColor()
虽然OP特别要求iOS 9,但以下内容可确保iOS 9之前的不弃用方法可以被称为:
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
}
如果你的最小目标是iOS 9,正如你在这里特别提出的那样,那么上面的内容将是多余的 - Xcode会通过警告告诉你,以及你的信息。
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 Mapkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!