【发布时间】:2021-09-02 15:03:58
【问题描述】:
无论顶视图是什么,我都需要根据特定条件显示所有视图之上的视图。我正在尝试以下代码:
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)
}
现在,如果视图被推送,这可以正常工作。但我无法在呈现的视图上方显示此 TopView。
感谢任何帮助!
【问题讨论】:
-
这是否回答了您的问题stackoverflow.com/a/63259094/12299030?
-
不,它不是@Asperi。我需要一个视图显示在顶部,而不考虑当前的顶视图是什么,是呈现或推送的顶视图等。这就像在所有视图的顶部显示一个加载器。