I’m attempting to create a customized scroll view which may notify the mother or father view if the person has scrolled to the tip.
That is how I implement this:
struct CustomVerticalScrollView<Content material: View>: View {
let content material: Content material
init(@ViewBuilder content material: () -> Content material) {
self.content material = content material()
}
var physique: some View {
ScrollView {
content material
Colour
.clear
.body(width: 0, top: 0)
.modifier(ViewAppearsOnScreen())
.onPreferenceChange(IsOnScreenKey.self) { worth in
if worth == true {
print("Has reached finish!")
}
}
}
.background(.inexperienced)
}
}
struct ViewAppearsOnScreen: ViewModifier {
func physique(content material: Content material) -> some View {
GeometryReader { geometry in
// Examine if this view's body intersects with the seen display bounds
content material
.desire(
key: IsOnScreenKey.self,
worth: isViewVisible(geometry: geometry)
)
}
}
// Determines if the view is seen inside the display's bounds
personal func isViewVisible(geometry: GeometryProxy) -> Bool {
let body = geometry.body(in: .world) // Get the body in world area
let screenBounds = UIScreen.important.bounds // Display boundaries
return screenBounds.intersects(body) // Examine if it intersects with display bounds
}
}
The code used right here to detect the tip of the scroll view was taken from this reply
After which that is how I take advantage of it:
struct ContentView: View {
var physique: some View {
CustomVerticalScrollView{
VStack(spacing: .zero) {
Colour
.blue
.body(maxWidth: .infinity)
.body(top: 1000)
}
}
}
}
The performance works properly, nevertheless, This offers me the next outcomes with some further spacing:
- When I haven’t got clear shade view, it does not appear so as to add any spacing on the finish, though there is no such thing as a padding, spacing and the view’s top is 0 (third merchandise within the desk)
- After I add the clear shade view, it provides some appreciable spacing on the finish (first merchandise within the desk)
- If I take away the 2 talked about modifiers from the second merchandise within the desk, there may be nonetheless some padding – in all probability the Geometry reader is at play right here
I although setting the peak to 0 and utilizing the Geometry reader wouldn’t affect the spacing.
Simply questioning how can I remove all this further spacing in order that I can have this extra view in there.