如何停止在Swiftui滚动时坚持ScrollView的大型导航标题

发布于 2025-02-11 09:16:27 字数 237 浏览 1 评论 0原文

我希望能够在滚动视图上拖动,并且大型导航标题不得粘在内容时,因为它在向下滚动时隐藏了视图。

如何禁用这种行为?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2025-02-18 09:16:27

我能够修复此行为的方式是在层次结构中添加假视图,以便滚动视图不是屏幕的基本视图,就像如果滚动浏览是基本视图,它会自动添加这种粘性行为。只需添加普通的vstackemptleView似乎也无法正常工作,因为它可以说滚动浏览仍然是基本视图。

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

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

流年里的时光 2025-02-18 09:16:27

它应该与代表一样起作用,就像

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 {

    }
}

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 {

    }
}
尛丟丟 2025-02-18 09:16:27

您可以添加视图以填充屏幕,例如color,然后将您的scrollview应用于覆盖

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文