ScrollView 不是从列表的开头开始,也不是从列表的末尾开始

ScrollView is not starting from beginning nor reaching end of list

提问人:user13919384 提问时间:11/17/2023 最后编辑:user13919384 更新时间:11/17/2023 访问量:40

问:

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

垂直滚动视图:

  • 水平 ScrollView:

    1. SampleProfileView(示例配置文件视图)

    2. 示例配置文件视图B

组件1:

@ViewBuilder
func SampleProfileView() -> some View{
    var arrayData: [Int] = Array(1…50);
    LazyVStack{
        Text("About")
            .font(.custom("PlayfairDisplay-Bold", size: 25))
            .fontWeight(.bold)
            .frame(maxWidth: .infinity, alignment: .leading)
            .foregroundColor(.black)
            .padding(.horizontal)
        
        VStack{
            ForEach(arrayData, id: \.self){item in
                ZStack{
                    RoundedRectangle(cornerRadius: 15)
                        .fill(color.gradient)
                    //                        .frame(height: 100)
                    
                    Text("\(item)")
                        .font(.custom("PlayfairDisplay-Bold", size: 30))
                        .foregroundColor(.black)
                        .fontWeight(.bold)
                        .padding(.horizontal)
                }
                
            }
        }
    }
    
}

以下是组件 2:

@ViewBuilder
func SampleProfileViewB() -> some View{
    var arrayData: [Int] = Array(1…50);
    LazyVStack{
        Text("About")
            .font(.custom("PlayfairDisplay-Bold", size: 25))
            .fontWeight(.bold)
            .frame(maxWidth: .infinity, alignment: .leading)
            .foregroundColor(.black)
            .padding(.horizontal)
        
        VStack{
            ForEach(arrayData, id: \.self){item in
                ZStack{
                    RoundedRectangle(cornerRadius: 15)
                        .fill(color.gradient)
                    //                        .frame(height: 100)
                    
                    Text("\(item)")
                        .font(.custom("PlayfairDisplay-Bold", size: 30))
                        .foregroundColor(.black)
                        .fontWeight(.bold)
                        .padding(.horizontal)
                }
                
            }
        }
    }
    
}

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

    NavigationStack{
        ScrollView(.vertical){
            LazyVStack{
                TopProfileHeaderView()
                
                
                ScrollView(.horizontal){
                    LazyHStack{
                        
                        SampleProfileView()
                            .id(0)
                            .containerRelativeFrame(.horizontal)
                        
                        
                        SampleProfileViewB()
                            .id(1)
                            .containerRelativeFrame(.horizontal)
                       
                    }
                    
                    
                }
                .scrollIndicators(.visible)
                .scrollTargetBehavior(.paging)
                .scrollTargetLayout()
            }
            
        }
    }

图片 1:正如您在第一张图片中看到的,在顶部,它不会从列表的开头开始(即 0、1、2、3、4、5),也不会向上滚动到列表的开头。

图2:在第二张图片中,您可以看到当我滚动时,它没有到达列表的末尾(列表中的最后一个数字是 50)。

截图:

image_1 image_2

我的问题:

我的问题是,是什么阻止了列表达到全高并到达列表中的第一项和列表中的最后一项。

我的代码有问题吗?

感谢您的反馈。 谢谢。

iOS Swift Xcode SwiftUI 移动开发

评论

0赞 JanMensch 11/17/2023
嗯,当我将其添加到一个空项目并在 iOS 17 模拟器上运行它时,滚动视图对我来说从顶部开始。此外,我可以在两个列表中向下滚动到 50。您在使用哪个iOS版本时遇到了问题?

答:

0赞 JanMensch 11/17/2023 #1

对问题 1 的回答:

您需要定义需要使用的对齐方式。 如果两个不同的部分具有不同的高度,则需要告诉 SwiftUI 如何在 .默认情况下,它使用 .这意味着两种不同视图中的一种不一致,这可能会导致两个问题。LazyHStackLazyHStack.center

若要解决此问题,请使用:

LazyHStack(alignment: .top) { ... }

然后,较小和较高的视图都将在顶部对齐,该问题将得到解决。

但是,这仍然无法解决问题#2。另外,老实说,我不确定第二个问题是否有解决方案。