[英]How to make whole View scrollable with VStack placed above a List
目前我的视图只允许列表本身可以滚动。 有谁知道如何通过放置在 VSTACK 下方的列表使整个视图可滚动?
var body: some View {
VStack {
ZStack{
Circle()
.stroke(Color.black, lineWidth: 5)
.frame(width: 150, height: 150)
Text("Score")
.font(.system(size: 20))
.bold()
}.padding(.bottom, 30)
Text("Friends")
.bold()
.font(.system(size: 24))
List{
ForEach(allFriends) { item in
HStack{
ZStack{
Circle()
.fill(LinearGradient(gradient: Gradient(colors: [Color("LightBlue"), Color("LightPink"), Color("LightYellow")]), startPoint: .bottom, endPoint: .top))
.frame(width: 60, height: 60)
Text(item.emoji)
.font(.system(size: 40))
}.padding(.trailing, 40)
Text(item.name)
.font(.system(size: 20))
.bold()
}
}
}
}
}
}
struct ProfilePage_Previews: PreviewProvider {
static var previews: some View {
ProfilePage()
}
}
这里有 2 个可能的解决方案。 (1) 将 header 放在 List 中,或者 (2) 使用 ScrollView 而不是 List。 (您必须将 ForEach 循环更改回您的数据数组。)
struct ProfilePage: View {
var body: some View {
putHeaderWithinList
// OR
//useScrollViewInsteadOfList
}
var putHeaderWithinList: some View {
VStack {
List{
VStack {
ZStack{
Circle()
.stroke(Color.black, lineWidth: 5)
.frame(width: 150, height: 150)
Text("Score")
.font(.system(size: 20))
.bold()
}.padding(.bottom, 30)
Text("Friends")
.bold()
.font(.system(size: 24))
}
.frame(maxWidth: .infinity)
ForEach(0..<10) { index in
HStack{
ZStack{
Circle()
.fill(LinearGradient(gradient: Gradient(colors: [Color("LightBlue"), Color("LightPink"), Color("LightYellow")]), startPoint: .bottom, endPoint: .top))
.frame(width: 60, height: 60)
Text("\(index)")
.font(.system(size: 40))
}.padding(.trailing, 40)
Text("\(index)")
.font(.system(size: 20))
.bold()
}
}
}
}
}
var useScrollViewInsteadOfList: some View {
ScrollView {
VStack {
ZStack{
Circle()
.stroke(Color.black, lineWidth: 5)
.frame(width: 150, height: 150)
Text("Score")
.font(.system(size: 20))
.bold()
}.padding(.bottom, 30)
Text("Friends")
.bold()
.font(.system(size: 24))
ForEach(0..<10) { index in
VStack {
Divider()
HStack{
ZStack{
Circle()
.fill(LinearGradient(gradient: Gradient(colors: [Color("LightBlue"), Color("LightPink"), Color("LightYellow")]), startPoint: .bottom, endPoint: .top))
.frame(width: 60, height: 60)
Text("\(index)")
.font(.system(size: 40))
}.padding(.trailing, 40)
Text("\(index)")
.font(.system(size: 20))
.bold()
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.