Created
June 17, 2024 04:43
-
-
Save INCHMAN1900/9e8d1e3c6c2b8d4c1544ab53b5b09f0c to your computer and use it in GitHub Desktop.
ScrollView with divider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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