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 に答える