SwiftUI NavigationLink Button is gray and untoucha

2019-08-28 23:10发布

I can not get a NavigationLink in SwiftUI. The button is visible, but unfortunately this is gray and can not be clicked.

Here is the code:

import SwiftUI
import Combine

struct ContentView: View {
    var body: some View {
        NavigationView{
            NavigationLink(destination: Text("Detail for Test")) {
                    Text("Test")
            }.navigationBarTitle("Select a user")
        }
    }
}

Does anyone know the problem?

2条回答
对你真心纯属浪费
2楼-- · 2019-08-29 00:02

It seems the problem is related to the fact that you have added the NavigationLink inside the NavigationView without defining a layout

In fact, if you add a VStack, everything works correctly

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: Destination()) {
                        Text("Test")
                }
            }.navigationBarTitle("Select a user")
        }
    }
}

struct Destination: View {
    var body: some View {
        Text("Ok")
    }
}
查看更多
三岁会撩人
3楼-- · 2019-08-29 00:08

The error is related to a missing view in your NavigationLink statement.

...

NavigationLink(destination: NeedsAView()) {
    Text("Test")
}.navigationBarTitle("Select a user")

...

struct NeedsAView: View {
    var body: some View {
        Text("Hello Destination")
    }
}
查看更多
登录 后发表回答