[英]How to render a parent view in a child view in SwiftUI?
我正在开发一个迷你 iOS 项目,其中我的内容视图在内容视图(根视图)上呈现自定义 map 视图。
我想要实现的是,在一个可以通过导航链接访问的子视图中,我想在内容视图中显示 map 视图的一部分,而无需重新初始化新的 map 视图。 它是 3d map(有点重且昂贵),因此在子视图中重新初始化不是一个好主意。
我想在 SwiftUI 中实现什么?
struct ContentView : View {
public var myMapViewObj = myMapView()
var body: some View {
NavigationView{
ZStack{
myMapViewObj
// Something like this
NavigationLink(destination: someChildView(fromParentView: myMapViewObj))
}
}
}
}
struct someChildView: View {
public var fromParentView : View
var body: some View {
VStack {
myMapViewObj // <- from ContentView
.frame(height: UIScreen.main.bounds.height * 0.5)
SomeOtherView()
}
}
}
任何意见将不胜感激。 谢谢
如果您的 myMapViewObj 是 View 类型,那么您可以将它与视图约束一起使用。
struct SomeChildView<Content: View>: View { //<--Here
public var fromParentView : Content //<--Here
var body: some View {
VStack {
myMapViewObj // <- from ContentView
.frame(height: UIScreen.main.bounds.height * 0.5)
SomeOtherView()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.