代码之家  ›  专栏  ›  技术社区  ›  Mehul Patel Nikita Khandelwal

如果超出视图高度,则SwiftUI+ScrollView无法添加视图

  •  0
  • Mehul Patel Nikita Khandelwal  · 技术社区  · 4 年前

    我想展示一下 Text 文本 使用不同的样式和字体。为此,我添加了多个 单个视图中的组件。

    为了使这成为可能,我正在使用 ScrollView 组件和全部添加 文本

    代码:

    struct TextClass: View {
        var body: some View {
            GeometryReader { geometry in
                ScrollView(.vertical, showsIndicators: false) {
                    Text("This is suppose to be a really long text that can go on to multiple lines. By default, it could go more than one lines.").padding()
    
                    Text("This is only one line regardless of how long the sentence is")
                        .padding()
                        .lineLimit(1)
                    Text("Hello Swift - LargeTitle").font(.largeTitle).padding()
                    Text("Hello Swift - Title").font(.title).padding()
                    Text("Hello Swift - Headline").font(.headline).padding()
                    Text("Hello Swift - SubHeadline").font(.subheadline).padding()
                    Text("Hello Swift - body").font(.body).padding()
                    Text("Hello Swift- callout").font(.callout).padding()
                    Text("Hello Swift- Footnote").font(.footnote).padding()
    
                    Text("Font Weight - Ultralight").fontWeight(.ultraLight).padding()
                    Text("Font Weight - Thin").fontWeight(.thin).padding()
        //            Text("Font Weight - light").fontWeight(.light).padding()
        //            Text("Font Weight - Regular").fontWeight(.regular).padding()
                }.frame(width: geometry.size.width)
            }
        }
    }
    
    struct TextClass_Previews: PreviewProvider {
        static var previews: some View {
            TextClass()
        }
    }
    

    它显示的错误如下:

    enter image description here

    Text("Font Weight - Thin").fontWeight(.thin).padding()
    

    错误消失了。

    似乎只允许我加10 文本 组件在这种情况下,如果我添加更多,它会给我一个错误。

    是虫子吗?这是速腾的常见行为吗?如果scrollView不允许添加更多组件,它有什么好处?

    0 回复  |  直到 4 年前
        1
  •  3
  •   Shehata Gamal    4 年前

    你需要使用 Group 因为视图中的元素不能超过10个

    struct TextClass: View {
        var body: some View {
            GeometryReader { geometry in
                ScrollView(.vertical, showsIndicators: false) {
                        Group {
                                Text("This is suppose to be a really long text that can go on to multiple lines. By default, it could go more than one lines.").padding()
                                Text("This is only one line regardless of how long the sentence is")
                                    .padding()
                                    .lineLimit(1)
                                Text("Hello Swift - LargeTitle").font(.largeTitle).padding()
                                Text("Hello Swift - Title").font(.title).padding()
                                Text("Hello Swift - Headline").font(.headline).padding()
                                Text("Hello Swift - SubHeadline").font(.subheadline).padding()
                                Text("Hello Swift - body").font(.body).padding()
                        }
                        Group {
                                Text("Font Weight - Ultralight").fontWeight(.ultraLight).padding()
                                Text("Font Weight - Thin").fontWeight(.thin).padding()
                                Text("Font Weight - light").fontWeight(.light).padding()
                                Text("Font Weight - Regular").fontWeight(.regular).padding()
                        }
                }.frame(width: geometry.size.width)
            }
        }
    }