Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: NavigationLink not working if not in a List

Tags:

swift

swiftui

I guess it might be a bug in beta 3 as the NavigationView is all broken. But a view like that:

struct GenreBadge : View {
    @EnvironmentObject var store: Store<AppState>
    let genre: Genre

    var body: some View {
        NavigationLink(destination: MoviesGenreList(genre: genre).environmentObject(store)) {
            RoundedBadge(text: genre.name)
        }
    }
}

is not triggering any push in the navigation stack. The view doens't seems interactive at all. If anyone found a workaround would be good, unless Apple is documenting this behaviour I would consider it broken until beta 4.

like image 327
Dimillian Avatar asked Jul 05 '19 07:07

Dimillian


2 Answers

There seems to be a bug with the navigation link in Xcode version 11.3(11C29) which I have reported to Apple.

Note: This problem only appears in simulator. It works fine on a real device. Thanks to @djr

The below code works as expect the first time you use the navigation link. Unfortunately it becomes unresponsive the second time around.

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SomeView()) {
                    Text("Hello!")
                }
            }
        }
    }
}

struct SomeView: View {
    var body: some View {
        Text("Detailed View")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
like image 160
Aaron A Avatar answered Oct 17 '22 13:10

Aaron A


Are you actually inside a NavigationView? The following works. But if I missed your point, maybe you can share a little more code.

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SomeView()) {
                    Text("Go!")
                }
            }
        }
    }
}

struct SomeView: View {
    var body: some View {
        Text("Detailed View Here!")
    }
}
like image 21
kontiki Avatar answered Oct 17 '22 12:10

kontiki