1

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

Fonix
  • 11,447
  • 3
  • 45
  • 74

3 Answers3

2

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

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • As of Xcode 14 beta 3 this seems to have stopped working :( try one of the other solutions here – Fonix Jul 22 '22 at 10:59
1

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 {

    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Unfortunately I tried this and it doesnt seem to work, it somehow knows to use the scrollview for the sticky behaviour, it seems using empty swiftUI views instead of ones with content like a `Text` dont seem to be considered for the enabling and disabling of this feature for some reason, not sure what the mechanics of this are behind the screnes – Fonix Jun 27 '22 at 12:37
  • It intrigued me (because I used that before) - you're right, they again broken something. Anyway see updated tested variants (replicated based on this https://stackoverflow.com/a/72453592/12299030) – Asperi Jun 27 '22 at 13:31
0

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
    }
}
Kane Buckthorpe
  • 75
  • 2
  • 10