swift 调整水平滚动视图的大小以匹配不同列表的大小

uqjltbpv  于 2024-01-05  发布在  Swift
关注(0)|答案(1)|浏览(122)

我有一个iOS应用程序,在那里我有2个列表,我在屏幕上显示,基本上是一个垂直的滚动视图,里面是一个水平的滚动视图,有2个列表:
垂直滚动视图:

  • 顶部标题视图

1.水平滚动视图:
1.样本配置文件视图
1.样本配置文件视图B

  1. VStack(alignment: .center){
  2. VStack(alignment: .center){
  3. Image(systemName: "star.circle.fill")
  4. .resizable()
  5. .scaledToFit()
  6. .frame(width: 200, height: 200)
  7. .clipShape(.circle)
  8. Text("Some Guy")
  9. .font(.largeTitle)
  10. }.padding(.vertical)
  11. }

字符串
水平滚动视图:
组成部分1:

  1. @ViewBuilder
  2. func SampleProfileView() -> some View{
  3. var arrayData: [Int] = Array(110);
  4. LazyVStack{
  5. Text("About”)
  6. .fontWeight(.bold)
  7. .frame(maxWidth: .infinity, alignment: .leading)
  8. .foregroundColor(.black)
  9. .padding(.horizontal)
  10. VStack{
  11. ForEach(arrayData, id: \.self){item in
  12. ZStack{
  13. RoundedRectangle(cornerRadius: 15)
  14. .fill(color.gradient)
  15. // .frame(height: 100)
  16. Text("\(item)")
  17. .foregroundColor(.black)
  18. .fontWeight(.bold)
  19. .padding(.horizontal)
  20. }
  21. }
  22. }
  23. }
  24. }


构成部分2:

  1. @ViewBuilder
  2. func SampleProfileViewB() -> some View{
  3. var arrayData: [Int] = Array(150);
  4. LazyVStack{
  5. Text("About")
  6. .fontWeight(.bold)
  7. .frame(maxWidth: .infinity, alignment: .leading)
  8. .foregroundColor(.black)
  9. .padding(.horizontal)
  10. VStack{
  11. ForEach(arrayData, id: \.self){item in
  12. ZStack{
  13. RoundedRectangle(cornerRadius: 15)
  14. .fill(color.gradient)
  15. // .frame(height: 100)
  16. Text("\(item)")
  17. .foregroundColor(.black)
  18. .fontWeight(.bold)
  19. .padding(.horizontal)
  20. }
  21. }
  22. }
  23. }
  24. }


=主视图组件我在以下主视图组件中使用这两个组件:

  1. NavigationStack{
  2. ScrollView(.vertical){
  3. LazyVStack{
  4. TopHeaderView()
  5. ScrollView(.horizontal){
  6. LazyHStack{
  7. SampleProfileView()
  8. .id(0)
  9. .containerRelativeFrame(.horizontal)
  10. SampleProfileViewB()
  11. .id(1)
  12. .containerRelativeFrame(.horizontal)
  13. }
  14. }
  15. .scrollIndicators(.visible)
  16. .scrollTargetBehavior(.paging)
  17. .scrollTargetLayout()
  18. }
  19. }
  20. }

我的问题:

我的问题是,我如何让框架的高度调整到SampleProfileView()SampleProfileViewB()中不同列表大小的大小
正如你在图片中看到的,scrollview列表中的一些元素被隐藏了,我无法滚动。
正如你在下面的截图中看到的,它没有到达列表的末尾,它应该一直走到第50个元素,它只到达了第40个元素

截图:

image

e4eetjau

e4eetjau1#

回答我的问题。问题原来是因为我使用了LazyHStack。一旦我在**ScrollView(.horizontal)**中切换到HStack,它就被修复了,现在所有元素都显示出来了。

相关问题