SwiftUI unable to navigate back and forth with nav

2020-04-26 06:48发布

Notice in the gif that once I navigate and dismiss the new view, I am unable to navigate back! Is this a SwiftUI bug or a misuse of NavigationLinks?


struct ContentView: View {
    var body: some View {
        return NavigationView {
            NavigationLink(destination: FakeView1()) {
                Text("Navigate")
            }
        }
    }
}

struct FakeView1: View {
    var body: some View {
        Text("Hey")
    }
}

Video

2条回答
【Aperson】
2楼-- · 2020-04-26 07:36

it's simulators bug.Try with your device

查看更多
Animai°情兽
3楼-- · 2020-04-26 07:47

This seems swiftUI bug. I also faced the same issue so, I have used this workaround for it.

struct ContentView: View {

@State var isFakeActive: Bool = false

var body: some View {
    NavigationView {
        NavigationLink(destination: FakeView1(isFakeActive: self.$isFakeActive), isActive: self.$isFakeActive) {
            Text("Navigate")
        }
    }
  }
}

And for your FakeView1 class.

struct FakeView1: View {

@Binding var isFakeActive: Bool

var body: some View {
    Text("Hey")
        .navigationBarItems(leading: Button(action: {
            self.isFakeActive = false
        }, label: {
            HStack {
                Image(systemName: "arrow.left")
                Text("Back")
            }
        }))
    }
}

I have tested and it is working fine.

查看更多
登录 后发表回答