How to implement iOS 11's MKUserTrackingButton

╄→гoц情女王★ 提交于 2021-02-04 04:56:56

问题


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: Here's the WWDC 2017 session about this new feature (at 1:25): https://developer.apple.com/videos/play/wwdc2017/237/

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)])
}

Note that I have a mapView instance in my ViewController, of course.


回答1:


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.



来源:https://stackoverflow.com/questions/46766615/how-to-implement-ios-11s-mkusertrackingbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!