Swiftui Multiple Navigationlinks in Form/Sheet - Entry Stays Highlighted

SwiftUI prevent List in Sheet from inheriting parent Form styles

Use DefaultListStyle()

ListView().listStyle(DefaultListStyle())

TabView resets navigation stack when switching tabs

Here's a simple example of how to preserve state for a navigation stack with a list of items at the root:

struct ContentView: View {

var body: some View {

TabView {

Text("First tab")
.tabItem { Image(systemName: "1.square.fill"); Text("First") }
.tag(0)

SecondTabView()
.tabItem { Image(systemName: "2.square.fill"); Text("Second") }
.tag(1)
}
}
}

struct SecondTabView: View {

private struct ListItem: Identifiable {
var id = UUID()
let title: String
}

private let items = (1...10).map { ListItem(title: "Item #\($0)") }

@State var selectedItemIndex: Int? = nil

var body: some View {

NavigationView {
List(self.items.indices) { index in
NavigationLink(destination: Text(self.items[index].title),
tag: index,
selection: self.$selectedItemIndex) {
Text(self.items[index].title)
}
}
.navigationBarTitle("Second tab", displayMode: .inline)
}
}
}

SwiftUI's NavigationLink `tag` and `selection` in NavigationView stops working if not all NavigationLinks are displayed

This happens because lower items are not rendered, so in the hierarchy there's no NavigationLink with such tag

I suggest you using an ZStack + EmptyView NavigationLink "hack".

Also I'm using LazyView here, thanks to @autoclosure it lets me pass upwrapped currentSelection: this will only be called when NavigationLink is active, and this is happens when currentSelection != nil

struct ContentView: View {
@State var entries = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
@State var currentSelection: Int? = nil

var body: some View {
NavigationView {
ZStack {
EmptyNavigationLink(
destination: { DetailView(entry: $0) },
selection: $currentSelection
)
Form {
ForEach(entries.sorted(), id: \.self) { entry in
NavigationLink(
destination: DetailView(entry: entry),
label: { Text("The number \(entry)") })
}
}
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarLeading) { Button("Add low") {
let newEntry = (entries.min() ?? 1) - 1
entries.insert(newEntry, at: 1)
currentSelection = newEntry
} }
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) { Button("Add high") {
let newEntry = (entries.max() ?? 50) + 1
entries.append(newEntry)
currentSelection = newEntry
} }
ToolbarItem(placement: ToolbarItemPlacement.bottomBar) {
Text("The current selection is \(String(describing: currentSelection))")
}
}
}
}
}
}

struct DetailView: View {
let entry: Int
var body: some View {
Text("It's a \(entry)!")
}
}

public struct LazyView<Content: View>: View {
private let build: () -> Content
public init(_ build: @autoclosure @escaping () -> Content) {
self.build = build
}
public var body: Content {
build()
}
}

struct EmptyNavigationLink<Destination: View>: View {
let lazyDestination: LazyView<Destination>
let isActive: Binding<Bool>

init<T>(
@ViewBuilder destination: @escaping (T) -> Destination,
selection: Binding<T?>
) {
lazyDestination = LazyView(destination(selection.wrappedValue!))
isActive = .init(
get: { selection.wrappedValue != nil },
set: { isActive in
if !isActive {
selection.wrappedValue = nil
}
}
)
}

var body: some View {
NavigationLink(
destination: lazyDestination,
isActive: isActive,
label: { EmptyView() }
)
}
}

Check out more about LazyView, it helps often with NavigationLink: in real apps destination may be a huge screen, and when you have a NavigationLink in each cell SwiftUI will process all of them which may lead to lags



Related Topics



Leave a reply



Submit