如何解决在SwiftUI中的makeUIView中设置的值中访问GeometryReader
所以我已经使用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成为该函数。有谁知道该怎么做?
解决方法
您可以将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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。