SwiftUI 如何使用 LazyGrid 使 Text 元素不被截断?

问题描述 投票:0回答:1

使用下面的代码,文本将被截断,如图所示。

考虑到可变的文本长度,我希望所有文本都显示而不被截断。

struct ContentView: View {
    
    let data = ["O Menino","The Boy", "The Girl", "A Menina","Mae","Mother"]
    
    let layout = [
        GridItem(.adaptive(minimum:50))
        
    ]
    
    var body: some View {
       
        ScrollView{
            LazyVGrid(columns: layout, spacing: 20){
                ForEach(data, id: \.self){ item in
                    VStack{
                        Text(item).lineLimit(1)
                    }.background(Color.red)
                }
            }
        }
    }
}

enter image description here

swiftui swiftui-foreach
1个回答
6
投票

它是VGrid,它垂直生长填充列。在你的情况下,它只是一列。

如果您想让所有这些内容适合屏幕,您需要增加网格列的数量,例如

let data = ["O Menino","The Boy", "The Girl", "A Menina","Mae","Mother"]
let layout = Array(repeating: GridItem(.adaptive(minimum:50)), count: 4)

demo

© www.soinside.com 2019 - 2024. All rights reserved.