Fonix
Fonix

Reputation: 11597

How to stop large navigation title sticking to scrollview when scrolling down in SwiftUI

I want to be able to drag down on the scroll view and the large navigation title must not stick to the content as it is hiding a view when scrolling down.

How can I disable this behaviour?

drag down on scrollview behaviour demonstration

Upvotes: 1

Views: 1539

Answers (3)

Kane Buckthorpe
Kane Buckthorpe

Reputation: 105

You can add a view to fill the screen such as a Color, and the apply your ScrollView as an overlay

Color.clear.overlay {
    ScrollView {
        //Content
    }
}

Upvotes: 0

Asperi
Asperi

Reputation: 257819

It should work w/o representable, like

VStack(spacing: 0) {
    Rectangle().fill(.background).frame(height: 1)  // iOS 15+ !!

// backward-compatible variant
//    Color(uiColor: UIColor.systemBackground).frame(height: 1)
            
    // Your previous root scrollview
    ScrollView {

    }
}

Upvotes: 2

Fonix
Fonix

Reputation: 11597

The way I was able to fix this behaviour is to add a fake view to the hierarchy so that the scrollview is not the base view of the screen, as it seems if the scrollview is the base view it automatically adds this sticky behaviour. Just adding a plain VStack or EmptyView does not seem to work either as its able to tell that the scrollview is somehow still the base view.

VStack {
    // Stops large navigation titles from sticking to the scrollview if the scroll view is the base view
    FakeView().fixedSize()
            
    // Your previous root scrollview
    ScrollView {

    }
}

struct FakeView: UIViewRepresentable {
    
    public func makeUIView(context: UIViewRepresentableContext<Self>) -> UIView {
        UIView()
    }
    
    public func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<Self>) {
        
    }
}

fixed behaviour result demonstration

Upvotes: 2

Related Questions