访问SwiftUI中makeUIView中设置的值中的GeometryReader



所以我使用UIViewRepresentable协议和以下函数绘制了一个Mapbox Map:

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填充成为其函数。有人知道怎么做吗?

您可以将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
}

最新更新