所以我已经使用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 padding成为该函数。有谁知道该怎么做?
答案 0 :(得分:2)
您可以将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
}