【问题标题】:Change color pin iOS 9 Mapkit更改颜色引脚 iOS 9 Mapkit
【发布时间】:2015-09-28 03:39:31
【问题描述】:

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

【问题讨论】:

    标签: ios swift swift2 ios9 mapkitannotation


    【解决方案1】:

    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 也会通过警告告知您这一点,供您参考。

    【讨论】:

    • 如果应用仍需要支持 iOS 8,则不应进行此更改。
    • @rmaddy 但他是在 iOS 9 方面提出问题 - “我不知道如何更改 iOS 9 中引脚颜色的代码”
    • 这里的 OP 可能有点困惑,没有意识到他们可能仍然支持 iOS 8,这意味着没有什么可以改变的。我只是指出一种可能性,以防万一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多