Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to reset the MKMapView userTrackingMode when using SwiftUI is by creating a custom UIViewRepresentable for the MKMapView and implementing the updateUIView method:

struct MapView: UIViewRepresentable {
    var mapView = MKMapView()

    func makeUIView(context: Context) -> MKMapView {
        mapView.showsUserLocation = true
        return mapView
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        if uiView.userTrackingMode != .none {
            uiView.setUserTrackingMode(.none, animated: true)
        }
    }
}

In this example, the updateUIView method checks if the current userTrackingMode is not .none, and if so, it sets it to .none to reset the user's tracking mode. The animated parameter can be set to false if you don't want the mode change to be animated.