I have tried updating my app form NavigationView
to NavigationSplitView
.
Previously, my code looked like this:
// ContentView.swift
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Note.created, order: .reverse) var notes: [Note]
var body: some View {
NavigationView {
ScrollViewReader { scrollProxy in
ZStack() {
NoteListView(notes: notes)
NoteInputView(scrollProxy: scrollProxy)
.frame(maxHeight: .infinity, alignment: .bottom)
}
}
.navigationBarTitle(Text("Notes"))
}
}
}
The reason I am using a ZStack is because the NoteInputView
has a keyboard that should be dismissible on tap
. However, there is a bug that prevents swipeActions from receiving a tap if onTapGesture is used elsewhere, see description here:
https://stackoverflow.com/questions/77162726/swiftui-swipeactions-not-working-when-tap-gesture-is-used-elsewhere-focusstate
When using NavigationView, this behaves just fine:
Scrolled down:
Scrolled to top:
Using NavigationSplitView
, my code looks like this:
// ContentView.swift
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Note.created, order: .reverse) var notes: [Note]
@State var selection: Set<Note> = []
var body: some View {
ScrollViewReader { scrollProxy in
NavigationSplitView {
ZStack {
NoteListView(notes: notes, selection: $selection)
NoteInputView(scrollProxy: scrollProxy)
.frame(maxHeight: .infinity, alignment: .bottom)
}
.navigationTitle(Text("Notes"))
} detail: {
if let noteSelected = selection.first {
NoteView(note: noteSelected)
} else {
Text("No note selected")
.foregroundStyle(Color.secondary)
}
}
}
}
}
Where now the toolbar/title doesn't scroll appropriately:
Removing ZStack and putting the NoteInputView into an .overlay results in the same behavior. When I remove the ZStack and my NoteInput, the scrolling also works as desired. Wrapping the ZStack into a ScrollView hides the notes.
Seems to me that this might be a bug, or does anyone have another workaround in mind?