我正在尝试实现MKUserTrackingButton(iOS 11 SDK中的新增功能)。这个“按钮”实际上是从UIView继承的,因此我只是将UIView实例放在Map和Identity Inspector中,将其链接到MKUserTrackingButton类,并在我的代码和viewDidLoad()中添加了一个 socket 。
我通过以下方式对其进行初始化:
self.centerMapButton = MKUserTrackingButton.init(mapView: self.mapView)
但是,没有任何效果,我的 map 上只有空白 View 。
PS:这是关于此新功能的WWDC 2017 session (在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)])
}
注意,我的ViewController中当然有一个mapView实例。
最佳答案
我只是在MapKit应用上运行了您的代码,它运行良好。确保将正确的mapView实例传递给MKUserTrackingButton。