Navigationlink Doesn't Work with New .Searchable Modifier on Swiftui 3.0

NavigationLink inside .searchable does not work

NavigationLink must always be inside NavigationView, no matter what. If you want to put it outside, like inside .searchable, you should use programmatic navigation with isActive.

struct ContentView: View {

@State private var term = ""
@State private var isPresenting = false

var body: some View {
NavigationView {

/// NavigationView must only contain 1 view
VStack {
Text("Hello, world!")
.padding()

/// invisible NavigationLink
NavigationLink(destination: SecondView(), isActive: $isPresenting) { EmptyView()}
}
}
.searchable(text: $term) {
Button { isPresenting = true } label: {
Text("Go to second view")
}
}
}
}

struct SecondView: View {
var body: some View {
Text("Goodbye!")
.padding()
}
}

Result:

Working navigation link that only appears when search bar tapped

SwiftUI: NavigationTitle display not normal with List and search field

Eventually, I found a way to circumvent this problem on my own.

We can manually set the List Style:

.listStyle(InsetGroupedListStyle())

Then It shows normally.

SwiftUI: NavigationView focus on last selected NavigationLink

you could try this approach, using the List with selection, such
as in this example code. It does not scroll back to the beginning of the list
after selecting a destination.

struct ContentView: View {
@State private var selections = Set<Thing>()
@State var things: [Thing] = []

var body: some View {
NavigationStack {
List(things, selection: $selections){ thing in
NavigationLink(destination: Text("destination-\(thing.val)")) {
Text("item-\(thing.val)")
}
}
}
.onAppear {
(0..<111).forEach{things.append(Thing(val: $0))}
}
}
}

EDIT-1:

Since there are so many elements missing from you code, I can only guess
and suggest something like this:

struct CategoryDetailView: View {

@EnvironmentObject var blindzeln: BLINDzeln
@AppStorage ("version") var version: Int = 0

@State var shouldRefresh: Bool = false
@State private var searchText = ""

@State private var selections = Set<Thing>() // <-- same type as item in the List

let categoryTitle: String
let catID: Int

var body: some View {
VStack {
// -- here
List(blindzeln.results.filter { searchText.isEmpty || ($0.title.localizedCaseInsensitiveContains(searchText) || $0.textBody.localizedCaseInsensitiveContains(searchText)) },
id: \.entryID,
selection: $selections){ item in

NavigationLink(destination: ItemDetailViewStandard(item: item, isFavorite: false, catID: catID)) {
DisplayEntryView(item: item, catID: catID)
}
.listRowSeparatorTint(.primary).listRowSeparator(.hidden)
}
}
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: "") {}

.navigationTitle(categoryTitle)
.navigationBarTitleDisplayMode(.inline)
.listStyle(.inset)
.task{
await blindzeln.decodeCategoryData(showCategory: categoryTitle)
}
.onAppear{
blindzeln.resetData()
}
}

}

Is there a way to tell if the search bar is active with the .searchable modifier?

So the environment value is isSearching. Not exactly easy to use since the declaration of the property must be in a subview of the view that is searchable.



Related Topics



Leave a reply



Submit