如何使 SwiftUI 视图可滚动?

12

如何使我的Body视图可滚动?

我需要使大型Lorem ipsum文本具备滚动功能。

struct FeatureDetail : View {
    var body: some View {  
       //Scrollable {  Does Not work
        VStack{
            Image("wwdc")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .padding()

            Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.").lineLimit(nil).padding(20)

            }.navigationBarTitle(Text("WWDC"), displayMode:.automatic)
       //}
    }
}        

使用列表而不是滚动视图。 - PinkeshGjr
2
ScrollView 对我来说可行,至少在 watchOS 上是这样。 - onmyway133
1个回答

14

你可以使用List而不是ScrollView来启用滚动。在List中包装你的内容:

List {
  VStack{
    Image("wwdc")
      .resizable()
      .aspectRatio(contentMode: .fit)
      .padding()

    Text("Very long text").lineLimit(nil).padding(20)

  }.navigationBarTitle(Text("WWDC"), displayMode:.automatic)
}

目前,ScrollView 在处理 Text 时存在问题,因为它会导致水平滚动。 - M Reza
使用List是一个很好的解决方法,还是未来的正确解决方案? - funkenstrahlen
2
@funkenstrahlen 这只是一个权宜之计。我们必须等待下一个版本发布,看看是否已经修复了这个问题。 - M Reza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接