0

所以我使用 UIViewRepresentable 协议和以下函数渲染了一个 Mapbox 地图:

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
}

唯一的问题是,因为我对 y 边距的值进行了硬编码,所以跨多个设备的定位并不理想。我想使用 GeometryReader 来访问 safeAreaInsets,然后使 y 填充成为它的功能。有谁知道如何做到这一点?

4

1 回答 1

4

您可以作为视图可表示构造函数的参数传递GeometryProxy,如下所示

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
}
于 2020-08-20T16:46:08.897 回答