嵌套导航堆栈中最后打开的黄色视图不会关闭。我怎么解决这个问题?帮助。
struct ContentView: View
{
@State private var openRed = false
var body: some View
{
NavigationStack
{
VStack
{
Button("Open Red") {
openRed = true
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
.navigationBarBackButtonHidden()
.navigationDestination(isPresented: $openRed)
{
Red(isOpen: $openRed)
}
}
}
}
struct Red: View
{
@Binding var isOpen: Bool
@State private var openYellow = false
var body: some View
{
NavigationStack
{
VStack
{
Button("Close") {
isOpen = false
}
Button("Open Yellow") {
openYellow = true
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)
.navigationBarBackButtonHidden()
.navigationDestination(isPresented: $openYellow)
{
Yellow(isOpen: $openYellow)
}
}
}
}
struct Yellow: View
{
@Binding var isOpen: Bool
var body: some View
{
VStack
{
Button("Close") {
isOpen = false
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.yellow)
.navigationBarBackButtonHidden()
}
}
我想要关闭最后打开的黄色视图,但它没有关闭。另外,打开黄色视图时没有动画效果
我想要关闭最后打开的黄色视图,但它没有关闭。另外,打开黄色视图时没有动画效果
我想要关闭最后打开的黄色视图,但它没有关闭。另外,打开黄色视图时没有动画效果
您不需要在红色中添加另一个导航堆栈,因为您已经从红色视图中删除了基本导航导航堆栈。最好使用导航视图,所以黄色将是
struct Yellow: View { @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> var body: some View { VStack { Button("Close") { presentationMode.wrappedValue.dismiss() } } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.yellow) } }