如何实现 iOS 11 的 MKUserTrackingButton

Flo*_* P. 5 mapkit swift swift4 ios11

我正在尝试实现一个 MKUserTrackingButton(这是 iOS 11 SDK 中的新功能)。这个“按钮”实际上是从 UIView 继承的,所以我只是在我的 Map 和 Identity Inspector 中放置了一个 UIView 实例,将它链接到 MKUserTrackingButton 类,在我的代码和 viewDidLoad() 中添加了一个出口。

我用以下方式初始化它:

self.centerMapButton = MKUserTrackingButton.init(mapView: self.mapView)
Run Code Online (Sandbox Code Playgroud)

但是,没有任何效果,我的地图上只有一个空白视图。

PS:这是关于这个新功能的 WWDC 2017 会议(在 1:25):https : //developer.apple.com/videos/play/wwdc2017/237/

这是我在 viewDidLoad() 中调用以实现按钮的方法:

func setupUserTrackingButtonAndScaleView() {
    mapView.showsUserLocation = true

    let button = MKUserTrackingButton(mapView: mapView)
    button.layer.backgroundColor = UIColor(white: 1, alpha: 0.8).cgColor
    button.layer.borderColor = UIColor.white.cgColor
    button.layer.borderWidth = 1
    button.layer.cornerRadius = 5
    button.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(button)

    let scale = MKScaleView(mapView: mapView)
    scale.legendAlignment = .trailing
    scale.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(scale)

    NSLayoutConstraint.activate([button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
                                 button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
                                 scale.trailingAnchor.constraint(equalTo: button.leadingAnchor, constant: -10),
                                 scale.centerYAnchor.constraint(equalTo: button.centerYAnchor)])
}
Run Code Online (Sandbox Code Playgroud)

请注意,当然,我的 ViewController 中有一个 mapView 实例。

小智 2

我刚刚针对我的 MapKit 应用程序运行了您的代码,并且运行良好。确保将正确的 mapView 实例传递给 MKUserTrackingButton。