如何检查SwiftUI视图是以模式显示还是推送到导航堆栈上?
我试图在视图上有条件地显示一个自定义按钮,只要它是以模式显示的。这可能是,但我正试图在SwiftUI中实现这一点 我尝试使用环境变量如何检查SwiftUI视图是以模式显示还是推送到导航堆栈上?,swiftui,Swiftui,我试图在视图上有条件地显示一个自定义按钮,只要它是以模式显示的。这可能是,但我正试图在SwiftUI中实现这一点 我尝试使用环境变量presentationMode来检查它是否被显示,但该标志在两种情况下都是真的: @Environment(\.presentationMode) private var presentationMode if presentationMode.wrappedValue.isPresented { // true in both cases ... }
presentationMode
来检查它是否被显示,但该标志在两种情况下都是真的:
@Environment(\.presentationMode) private var presentationMode
if presentationMode.wrappedValue.isPresented { // true in both cases
...
}
有没有一种方法可以让视图知道它是被呈现还是被推送
额外上下文:
我正在尝试创建一个自定义修改器,该修改器将在两种场景中都重用的视图上自动添加“取消”按钮:
struct OverlayDismissButtonModifier: ViewModifier {
@Environment(\.presentationMode) private var presentationMode
func body(content: Content) -> some View {
content
.overlay(
Group {
if presentationMode.wrappedValue.isPresented { // <-- True in both cases :(
Button(action: { presentationMode.wrappedValue.dismiss() }) {
Label(LocalizedStringKey("Close"), systemImage: "xmark")
.labelStyle(IconOnlyLabelStyle())
.foregroundColor(Color(.label))
.padding(8)
.background(
Circle()
.fill(Color(.systemBackground).opacity(0.8))
)
}
.padding([.top, .trailing])
}
},
alignment: .topTrailing
)
}
}
extension View {
func overlayDismissButton() -> some View {
modifier(OverlayDismissButtonModifier())
}
}
struct overlydismissbuttonmodifier:ViewModifier{
@环境(\.presentationMode)私有变量presentationMode
func正文(内容:内容)->某些视图{
内容
.覆盖(
团体{
如果presentationMode.wrappedValue.isPresented{//某些视图{
修改器(OverlyDismissButtonModifier())
}
}
在modally vs inside navigation view添加视图时,您不能传递另一个自定义环境变量吗?我正在尝试创建一个自定义修饰符,该修饰符可以附加到在这两种场景中重复使用的视图上。它用于在显示时添加自定义关闭按钮,或者让系统创建导航bac我为更多的上下文添加了自定义修改器代码。你应该在视图模型中跟踪它。