gpt4 book ai didi

ios - SwiftUI 查看所有 View ,包括工作 TableView

转载 作者:行者123 更新时间:2023-12-05 08:13:06 24 4
gpt4 key购买 nike

我需要根据某些条件在所有 View 之上显示一个 View ,无论顶 View 是什么。我正在尝试以下代码:

    struct TestView<Presenting>: View where Presenting: View {

/// The binding that decides the appropriate drawing in the body.
@Binding var isShowing: Bool
/// The view that will be "presenting" this notification
let presenting: () -> Presenting

var body: some View {

GeometryReader { geometry in
ZStack(alignment: .top) {
self.presenting()

HStack(alignment: .center) {
Text("Test")
}
.frame(width: geometry.size.width - 44,
height: 58)
.background(Color.gray.opacity(0.7))
.foregroundColor(Color.white)
.cornerRadius(20)
.transition(.slide)
.opacity(self.isShowing ? 1 : 0)
}
}
}
}

extension View {
func showTopView(isShowing: Binding<Bool>) -> some View {
TestView(isShowing: isShowing,
presenting: { self })
}
}

struct ContentView: View {
@State var showTopView = false
NavigationView {
ZStack {
content
}
}
.showTopView(isShowing: $showTopView)

}

现在这在 View 被推送的情况下工作正常。但是我无法在呈现的 View 上方显示此 TopView。

感谢任何帮助!

最佳答案

这是实现你的目标的一种方法,你不需要绑定(bind),只需使用 let value。

enter image description here

struct ContentView: View {

@State private var isShowing: Bool = Bool()

var body: some View {

CustomView(isShowing: isShowing, content: { yourContent }, isShowingContent: { isShowingContent })

}

var yourContent: some View {

NavigationView {

VStack(spacing: 20.0) {

Text("Hello, World!")

Button("Show isShowing Content") { isShowing = true }

}
.navigationTitle("My App")

}

}

var isShowingContent: some View {

ZStack {

Color.black.opacity(0.5).ignoresSafeArea()

VStack {

Spacer()

Button("Close isShowing Content") { isShowing = false }
.foregroundColor(.white)
.padding()
.frame(maxWidth: .infinity)
.background(Color.blue.cornerRadius(10.0))
.padding()

}

}

}

}

struct CustomView<Content: View, IsShowingContent: View>: View {

let isShowing: Bool
@ViewBuilder let content: () -> Content
@ViewBuilder let isShowingContent: () -> IsShowingContent

var body: some View {

Group {

if isShowing { ZStack { content().blur(radius: isShowing ? 5.0 : 0.0); isShowingContent() } }
else { content() }

}
.animation(.default, value: isShowing)

}
}

关于ios - SwiftUI 查看所有 View ,包括工作 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69032774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com