简体   繁体   中英

Marking makeUIView func makes view NOT conform to UIViewRepresentable?

I am trying to update my map's style URL immediately when the device changes from light/dark mode in my SwiftUI app.

The error below shows on every line where "mapView" is declared inside func makeUIView...

"Cannot use mutating getter on immutable value: 'self' is immutable. Mark method 'mutating' to make 'self' mutable"

I was instructed to put mapView inside of makeUIView but there is another func called setCenter() that needs to modify the mapView.

However, when I mark the method mutating the view does not conform to UIViewRepresentable and this error shows every where "mapView" is inside of makeUIView

What can I do to fix this?

struct MapboxMapViewRepresentable: UIViewRepresentable {
    @Environment(\.colorScheme) var colorScheme
                    
    func makeCoordinator() -> Coordinator {
        print("Make coordinator")
        return Coordinator(control: self)
    }
    
    var mapboxStyleUrl: String {
         "mapbox://styles/" + (colorScheme == .dark ? "cl0318cec0sdfdsfsfsfnrshos" : "ckwih3sdfsdfdfsfdfiz7403d0h")
    }
        
    lazy var mapView: MGLMapView = {
        return MGLMapView(frame: .zero, styleURL: URL(string:  mapboxStyleUrl)!)
    }()
        
// FUNC CANNOT BE MUTATING BECAUSE VIEW WILL NOT CONFORM TO UIVIEWREPRESENTABLE
    func makeUIView(context: Context) -> MGLMapView {
        mapView.showsUserLocation = true    
        mapView.delegate = context.coordinator
        mapView.allowsRotating = false
        mapView.direction =  0.0
        return  mapView
    }

  // NEEDS TO BE ABLE TO ACCESS mapView
    func setCenterAt(coordinate : CLLocationCoordinate2D) {
        mapView.setCenter(coordinate, zoomLevel: MapViewModel.startAtZoomLevel, animated: false)
    }
 
    func updateUIView(_ mapView: MGLMapView, context: Context) {
        //print("updateUIView")
        // clear spot data of other users only when zoomed out too far to see them
    }
}

A view struct cannot be modified from within itself and makeUIView is exactly a place to create a view (life cycle is managed by SwiftUI), so the fix is to move creation inside, like

func makeUIView(context: Context) -> MGLMapView {
    // HERE !!
    let mapView = MGLMapView(frame: .zero, styleURL: URL(string:  mapboxStyleUrl)!)

    mapView.showsUserLocation = true    
    mapView.delegate = context.coordinator
    mapView.allowsRotating = false
    mapView.direction =  0.0
    return  mapView
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM