我想要制作一个可滚动的视图,其中视图中的元素被固定到顶部.不过,固定在滚动视图顶部的元素从技术上讲并不是视图的标题.

我希望滚动视图中的元素停留在标题上方,当用户向下滑动时,标题上方的元素将消失到顶部,而标题保持固定在屏幕上(类似于Twitter个人资料视图).

我当前的代码没有将标题固定在顶部.

struct SwiftUIView: View {
    var body: some View {
        ScrollView {
            LazyVStack(pinnedViews: [.sectionHeaders]) {
                Color.blue.frame(width: 50, height: 500)
                Color.red.frame(width: 50, height: 500)
                Color.blue.frame(width: 50, height: 500)
                
                Section {
                    ScrollView {
                        //more content 
                    }
                } header: {
                    Text("My Messages")
                }
            }
        }
    }
}

更新

Section(header: Text("My Messages")) {
    // Elements that will scroll above the header
    Color.blue.frame(width: 50, height: 500)
    Color.red.frame(width: 50, height: 500)
    Color.blue.frame(width: 50, height: 500)
}

推荐答案

您当前的实现使用SectionpinnedViews: [.sectionHeaders],但内部的ScrollView可能会导致布局出现问题.您需要确保"My Messages"标题被固定,同时允许其他内容在其上方滚动.

Try and place the header ("My Messages") outside the Section so that it becomes a standalone element in the LazyVStack.
Add the content that should appear above the header as separate elements in the LazyVStack, and make sure the header element (i.e., "My Messages" Text view) is always visible and pinned to the top.

这将是:

struct SwiftUIView: View {
    var body: some View {
        ScrollView {
            LazyVStack(pinnedViews: [.sectionHeaders]) {
                // Elements that will scroll above the header
                Color.blue.frame(width: 50, height: 500)
                Color.red.frame(width: 50, height: 500)
                Color.blue.frame(width: 50, height: 500)

                // Pinned header
                Text("My Messages")
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .background(Color.white)
                    .padding()
                    .background(Color.gray.opacity(0.5))
                    .id("Header")

                // More content below the header
                ForEach(0..<20) { _ in
                    Color.green.frame(width: 50, height: 100)
                }
            }
        }
    }
}

The "My Messages" text is now a separate element within the LazyVStack, and should stay pinned at the top of the screen.
The colors (blue and red) above the header should scroll off the screen when you swipe downwards, while the header remains fixed.

Ios相关问答推荐

使用UIGraphicsBeginIMAext创建新图像会扭曲图像 colored颜色

Swift addtarget方法的默认参数不生效

SwiftUI中的视频时间轴

通过在SWIFT中不起作用的泛型传递协议一致性类型

如何在SwiftUI中以一定Angular 绘制直线(&A)

当 .searchable 修饰符处于活动状态时,如何将变量设置为 false?

当任何 TextEditor 有多于一行时,所有 TextEditor 都会调整大小

Toast 不显示 Flutter

我正在try 制作一种 Select 器样式,例如设置中的外观 Select 器

如何在 SwiftUI 中只显示一次随机数组的项目?

Xcode 13.4.1 (13F100)Command CodeSign 失败,退出代码非零我应该删除哪个证书?

检测 iPhone 应用程序中是否存在摄像头?

何时需要将应用程序源包含在测试目标中?

如何关闭情节提要弹出框

UIView 动画与 CALayers

针对 Xcode 中的 iPad 选项

iOS在应用程序中下载并保存图像

iPhone:什么是 WWDR 中级证书?

具有两种不同字体大小的 NSAttributedString 示例?

NSURL 到带有 XCTest 的测试包中的文件路径