11.9 C
London
Thursday, May 16, 2024

ios – Default area in SwiftUI ScrollView


I’m attempting to create a customized scroll view which might notify the father or mother view if the consumer has scrolled to the top.

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
            
            Shade
                .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
                .choice(
                    key: IsOnScreenKey.self,
                    worth: isViewVisible(geometry: geometry)
                )
        }
    }
    
    // Determines if the view is seen throughout the display's bounds
    non-public func isViewVisible(geometry: GeometryProxy) -> Bool {
        let body = geometry.body(in: .world) // Get the body in world area
        let screenBounds = UIScreen.primary.bounds // Display screen boundaries
        
        return screenBounds.intersects(body) // Examine if it intersects with display bounds
    }
}

The code used right here to detect the top 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) {
                Shade
                    .blue
                    .body(maxWidth: .infinity)
                    .body(top: 1000)
            }
        }
    }
}

The performance works nicely, nonetheless, This offers me the next outcomes with some additional spacing:

Custom scrollview swiftUI end of scroll reached

  1. When I haven’t got clear colour view, it does not appear so as to add any spacing on the finish, despite the fact that there isn’t any padding, spacing and the view’s top is 0 (third merchandise within the desk)
  2. Once I add the clear colour view, it provides some appreciable spacing on the finish (first merchandise within the desk)
  3. If I take away the 2 talked about modifiers from the second merchandise within the desk, there may be nonetheless some padding – most likely the Geometry reader is at play right here

I although setting the peak to 0 and utilizing the Geometry reader wouldn’t influence the spacing.

Simply questioning how can I get rid of all this additional spacing in order that I can have this extra view in there.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here