我想要制作一个可滚动的视图,其中视图中的元素被固定到顶部.不过,固定在滚动视图顶部的元素从技术上讲并不是视图的标题.
我希望滚动视图中的元素停留在标题上方,当用户向下滑动时,标题上方的元素将消失到顶部,而标题保持固定在屏幕上(类似于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)
}