我创建的PopupView是这样的:
import SwiftUI
struct PopupView<Content>: View where Content: View {
@Binding var isPresented: Bool
@Binding var viewForPresentation: Content
var content: () -> Content
var body: some View {
ZStack(alignment: .bottom) {
content()
.blur(radius: isPresented ? 3 : 0)
.disabled(isPresented)
HStack {
viewForPresentation
}
.opacity(isPresented ? 1 : 0)
}
}
}
现在我想在另一个视图中使用它,如下所示:
struct ServiceEditView: View {
@State var isPopupPresented: Bool
@State var viewForPopupPresentation: any View
private var firstView: some View {
Button {
isPopupPresented = false
} label: {
Text("First View")
}
}
private var secondView: some View {
Button {
isPopupPresented = false
} label: {
Text("Second View")
}
}
var body: some View {
PopupView(isPresented: $isPopupPresented, viewForPresentation: $viewForPopupPresentation) {
// ❌ Type 'any View' cannot conform to 'View'
HStack {
Button {
viewForPopupPresentation = firstView
isPopupPresented = true
} label: {
Text("1st")
}
Button {
viewForPopupPresentation = secondView
isPopupPresented = true
} label: {
Text("2nd")
}
}
}
}
}
是否有可能使用视图类型的绑定属性来呈现另一个视图?
我想用同一个单一的机制来呈现不同的观点,这取决于它来自哪里。
1条答案
按热度按时间htrmnn0y1#
您的方法并不遵循“状态”方法。一般来说,您应该基于状态控制
body
中的内容,同时存储希望在属性(var viewForPopupPresentation: any View
)中显示的视图。SwiftUI
已经做了类似的事情:使用.sheet(item:content:)
修饰符呈现一个新的Sheet。所以我会为您的情况做一些非常相似的事情。首先创建一个
PopupModifier
。现在,我们可以使用与
sheet
非常相似的修饰符。