ScrollKit icon indicating copy to clipboard operation
ScrollKit copied to clipboard

`ScrollViewHeader` behaves strangely with presentationDetents

Open morluna opened this issue 2 years ago • 3 comments

When presenting a sheet with presentation detents, the sticky header functionality breaks a bit because the header tries to grow to the top of the screen, but the presentationDetent cuts it off.

The stretchiness functionality doesn't necessarily need to work on small detents, but would be nice for the header to stick to the top.

https://user-images.githubusercontent.com/8826948/225060529-23c33686-3175-42c4-9839-867d77423f33.MP4

morluna avatar Mar 14 '23 15:03 morluna

@morluna I think that maybe the stretch should be disabled altogether when being presented in a sheet. Do you know if it's possible to detect whether or not the view is in a sheet?

danielsaidi avatar Mar 14 '23 17:03 danielsaidi

@danielsaidi On the parent view, you could just use .presentationDetents(:selection:) and a state property to know when it changed.

There isn't a way for nested views (like the scroll view with sticky header) to know what presentation detent it's on out of the box, but maybe you can created a custom environment property for it?

struct PresentationDetentPreferenceKey: PreferenceKey {
    static var defaultValue: PresentationDetent = .large
    static func reduce(value: inout PresentationDetent, nextValue: () -> PresentationDetent) {
        value = nextValue()
    }
}
struct PresentationDetentEnvironmentKey: EnvironmentKey {
    static let defaultValue: PresentationDetent = .large
}
extension EnvironmentValues {
    var presentationDetent: PresentationDetent {
        get { self[PresentationDetentEnvironmentKey.self] }
        set { self[PresentationDetentEnvironmentKey.self] = newValue }
    }
}

morluna avatar Mar 14 '23 18:03 morluna

I will fix this the next time I use the view in a way that causes this problem, but I'll be happy to review any PRs that fixes it.

danielsaidi avatar May 30 '24 08:05 danielsaidi