SwiftUI: NavigationDestinationLink deprecated

前端 未结 5 421
有刺的猬
有刺的猬 2020-12-01 09:54

After installing Xcode 11 beta 5 this morning, I noticed that NavigationDestinationLink was deprecated in favor of NavigationLink.

Also, t

相关标签:
5条回答
  • 2020-12-01 10:26

    To improve the workaround without replacing the back button with a custom one, you can use the code above :

    NavigationLink(destination: Test().onAppear(perform: {
        self.editPushed = nil
    }), tag: 1, selection: self.$editPushed) {
        Button(action: {
            self.editPushed = 1
        }) {
            Image(systemName: "plus.app.fill")
                .font(.title)
        }
    }
    

    The onAppear block will erase the selection value preventing to display the detail view twice

    0 讨论(0)
  • 2020-12-01 10:33

    Found NavigationLink answer Here .

    0 讨论(0)
  • 2020-12-01 10:43

    You can also use NavigationLink(destination:tag:selection)

    NavigationLink(destination: MyModal(), tag: 1, selection: $tag) {
        EmptyView()
    }
    

    So programmatically you can set tag to 1 in order to push MyModal. This approach has the same behaviour as the one with the Bool binding variable, so when you pop the first time it pushes the view immediately, hopefully they'll fix it in next beta.

    The only downside I see with this approach, compared to DynamicNavigationDestinationLink is that you need to provide a View to NavigationLink, even if you don't need one. Hopefully they'll find a cleaner way to allow us to push programmatically.

    0 讨论(0)
  • 2020-12-01 10:44

    The solution is to create custom back button for your detail view and pop detail view manually.

    .navigationBarItems(leading:
                    Button(action: {
                        self.showDetail = false
                    }) {
                        Image(systemName: "chevron.left").foregroundColor(.red)
                            .font(.system(size: 24, weight: .semibold))
                        Text("Back").foregroundColor(.red)
                        .font(.system(size: 19))
                    }
                )
    
    0 讨论(0)
  • 2020-12-01 10:46

    After spending some time with NavigationLink(destination:isActive), I am liking it a lot more than the old NavigationDestinationLink. The old view was a little confusing, while the new approach seems much more elegant. And once I figure out how to push without animations, it would make state restoration at application launch very easy.

    There is one problem though, a big and ugly bug. :-(

    Pushing a view programatically works fine, and popping it programatically does too. The problem starts when we use the BACK button in the pushed view which behaves oddly every other time. The first time the view is popped, the view pops and pushes again immediately. The second time around it works fine. Then the third time it starts all over again.

    I have created a bug report (number here). I recommend you do the same and reference my number too, to help Apple group them together and get more attention to the problem.

    I designed a workaround, that basically consists of replacing the default Back button, with our own:

    class Model: ObservableObject {
        @Published var pushed = false
    }
    
    struct ContentView: View {
        @EnvironmentObject var model: Model
    
        var body: some View {
            NavigationView {
                VStack {
                    Button("Push") {
                        // view pushed programmatically
                        self.model.pushed = true
                    }
    
                    NavigationLink(destination: DetailView(), isActive: $model.pushed) { EmptyView() }
                }
            }
        }
    }
    
    struct DetailView: View {
        @EnvironmentObject var model: Model
    
        var body: some View {
            Button("Bring me Back (programatically)") {
                // view popped programmatically
                self.model.pushed = false
            }
            // workaround
            .navigationBarBackButtonHidden(true) // not needed, but just in case
            .navigationBarItems(leading: MyBackButton(label: "Back!") {
                self.model.pushed = false
            })
        }
    }
    
    struct MyBackButton: View {
        let label: String
        let closure: () -> ()
    
        var body: some View {
            Button(action: { self.closure() }) {
                HStack {
                    Image(systemName: "chevron.left")
                    Text(label)
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题