简体   繁体   中英

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?

向下拖动滚动视图行为演示

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>) {
        
    }
}

固定行为结果演示

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 {

    }
}

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM