0

so I have rendered a Mapbox Map using the UIViewRepresentable protocol and the following function:

func makeUIView(context: Context) -> MGLMapView {
    
    let map = MGLMapView()
    DispatchQueue.main.async {
        map.styleURL = self.mapStyle
        map.delegate = context.coordinator
        map.showsUserLocation = true
        map.attributionButtonPosition = .topLeft
        map.logoViewPosition = .topLeft
        map.logoViewMargins.y = 15
        map.logoViewMargins.x = 95
        map.logoViewMargins.x = 12
        map.attributionButtonMargins.x = 100
        map.attributionButtonMargins.y = 15

        self.configure(map)
    }
    return map
}

The only problem is that because I am hardcoding the values for the y margin, the positioning is not ideal across multiple devices. I'd like to use a GeometryReader to access the safeAreaInsets and then make the y padding a function of that. Does anyone know how to do this?

nickcoding
  • 305
  • 8
  • 35

1 Answers1

4

You can pass GeometryProxy as argument of view representable constructor, like below

struct ContentView: View {
    var body: some View {
        GeometryReader {
            DemoView(proxy: $0)
        }
    }
}

struct DemoView: UIViewRepresentable {
   let proxy: GeometryProxy

   func makeUIView(context: Context) -> MGLMapView {
      // use self.proxy.size here
   }

   // ... other code
}
Asperi
  • 228,894
  • 20
  • 464
  • 690