问题描述
我正在尝试实现MKUserTrackingButton(iOS 11 SDK中的新增功能).这个按钮"实际上是从UIView继承的,所以我只是将一个UIView实例放在我的Map和Identity Inspector中,将其链接到MKUserTrackingButton类,在我的代码和viewDidLoad()中添加了一个插座.
I'm trying to implement a MKUserTrackingButton (which is new in iOS 11 SDK). This "button" actually inherits from UIView so I just put a UIView instance on my Map and in the Identity Inspector, linked it to the MKUserTrackingButton class, added an outlet in my code and in viewDidLoad().
我通过以下方式对其进行初始化:
I initialize it the following way:
self.centerMapButton = MKUserTrackingButton.init(mapView: self.mapView)
但是,什么都没用,我的地图上只有空白视图.
However, nothing works, I just have a blank view on my Map.
PS:这是有关此新功能的WWDC 2017会议(在1:25): https://developer.apple.com/videos/play/wwdc2017/237/
PS: Here's the WWDC 2017 session about this new feature (at 1:25): https://developer.apple.com/videos/play/wwdc2017/237/
这是我在viewDidLoad()中调用以实现按钮的方法:
Here's the method I call in viewDidLoad() to implement the button:
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实例.
Note that I have a mapView instance in my ViewController, of course.
推荐答案
我刚刚针对我的MapKit应用运行了您的代码,它运行良好.确保将正确的mapView实例传递给MKUserTrackingButton.
I just ran your code against my MapKit app and it worked fine. Make sure you are passing the proper mapView instance to the MKUserTrackingButton.
这篇关于如何实现iOS 11的MKUserTrackingButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!