Accessing GeometryReader in values set in makeUIView in SwiftUI

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?

Add Comment
1 Answer(s)

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 } 
Answered on August 31, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.