[英]Create a view over navigationView, SwiftUI
I'm creating a view embedded in a ZStack, something like this:我正在创建一个嵌入在 ZStack 中的视图,如下所示:
ZStack(alignment: .top) {
content
if self.show {
VStack {
HStack {
This is a viewModifier so I call this in my main view with for example: .showView()
.这是一个 viewModifier,所以我在我的主视图中调用它,例如: .showView()
。
But what happened is that if I have a NavigationView, this view is only showing below the navigationView.但是发生的事情是,如果我有一个 NavigationView,则该视图仅显示在 navigationView 下方。 (I have a navigationViewTitle that is over my view). (我有一个在我的视图上方的 navigationViewTitle)。
How can I solve this problem?我怎么解决这个问题? I was thinking about some zIndex but it is not working.我在考虑一些 zIndex 但它不起作用。 I thought also about some better placement of this .showView(), but nothing to do.我还考虑过更好地放置这个 .showView(),但无事可做。
Here is a demo of possible approach (it can be added animations/transitions, but it is out of topic).这是一个可能方法的演示(可以添加动画/过渡,但它不在主题范围内)。 Demo prepared & tested with Xcode 11.4 / iOS 13.4使用 Xcode 11.4 / iOS 13.4 准备和测试的演示
struct ShowViewModifier<Cover: View>: ViewModifier {
let show: Bool
let cover: () -> Cover
func body(content: Content) -> some View {
ZStack(alignment: .top) {
content
if self.show {
cover()
}
}
}
}
struct DemoView: View {
@State private var isPresented = false
var body: some View {
NavigationView {
VStack {
NavigationLink("Link", destination: Button("Details")
{ self.isPresented.toggle() })
Text("Some content")
.navigationBarTitle("Demo")
Button("Toggle") { self.isPresented.toggle() }
}
}
.modifier(ShowViewModifier(show: isPresented) {
Rectangle().fill(Color.red)
.frame(height: 200)
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.