Skip to content

Instantly share code, notes, and snippets.

@INCHMAN1900
Created June 17, 2024 04:43
Show Gist options
  • Save INCHMAN1900/9e8d1e3c6c2b8d4c1544ab53b5b09f0c to your computer and use it in GitHub Desktop.
Save INCHMAN1900/9e8d1e3c6c2b8d4c1544ab53b5b09f0c to your computer and use it in GitHub Desktop.
ScrollView with divider
struct SearchView: NSViewRepresentable {
typealias NSViewType = NSSearchField
func makeNSView(context: Context) -> NSSearchField {
return NSSearchField()
}
func updateNSView(_ nsView: NSSearchField, context: Context) {
}
}
class ScrollViewOffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {}
typealias Value = CGFloat
}
struct SidebarView: View {
@State private var scrollPosition: CGFloat = 0
var body: some View {
VStack {
SearchView()
Divider().opacity(scrollPosition < 0 ? 1 : 0)
ScrollView(content: {
ZStack {
HStack {}
GeometryReader(content: { geometry in
GeometryReader { proxy in
let offset = proxy.frame(in: .named("scroll")).minY
Color.clear.preference(key: ScrollViewOffsetPreferenceKey.self, value: offset)
}
})
}
.frame(height: 1)
.padding(0)
ForEach(0..<50, id: \.self, content: { item in
HStack(alignment: .center) {
Text("Item")
Spacer()
Text(item.description)
}
})
})
.frame(height: 300)
.coordinateSpace(name: "scroll")
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { value in
print("scroll position:", value)
scrollPosition = value
}
}
.padding(.horizontal, 16)
}
}
struct ContentView: View {
var body: some View {
SidebarView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment