Wednesday, October 7, 2026
HomeiOS Developmentios - UIViewRepresentable and Map SwiftUI

ios – UIViewRepresentable and Map SwiftUI

[ad_1]

I’m utilizing UIKit MKMapView in SwiftUI to have extra performance.
I go the map area to the UIViewRepresentable through Binding, so I can replace the UIKit view from the SwiftUI view that makes use of that MKMapView.
The difficulty is that I’ve a button on high of the map that’s answerable for centering the map again to the person location, however each time the map area modifications, the updateUIView perform triggers, and the map facilities it self, which results in the map being caught as a result of every time I transfer the map (i.e zoom, drag, rotate) the map facilities itself.

How can I conditionally test if the button was clicked – and solely then middle the map?


updateUIView:

func updateUIView(_ uiView: MKMapView, context: Context) {
    if let userLocation = uiView.annotations.first(the place: { $0 is MKUserLocation }) {
        uiView.setRegion(MKCoordinateRegion(middle: userLocation.coordinate, span: MapDetails.defaultSpan), animated: true)
    } // this will get triggered everytime map area modifications.
}

UIViewRepresentable:

struct MapView: UIViewRepresentable {
@Binding var mapRegion: MKCoordinateRegion
@Binding var areas: [Location]
...
}

Important SwiftUI View (The place I exploit the MKMapView):

struct ExploreView: View {


@StateObject personal var viewModel = MapViewModel()
@State personal var didAppearOnce = false

@ViewBuilder
var physique: some View {
    NavigationView {
        ZStack {
            MapView(mapRegion: $viewModel.mapRegion, areas: $viewModel.areas)
                .accentColor(.pink)
            ...
}

So my query is, is there any approach to conditionally test whether or not the button was pressed and solely then middle the map inside updateUIView ?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments