gpt4 book ai didi

SwiftUI LazyGrid 动态列宽

转载 作者:行者123 更新时间:2023-12-03 13:40:26 26 4
gpt4 key购买 nike

如何使 Grid 项目宽度动态化,以便它采用文本的宽度?
使用下面的代码截断文本,考虑到可变文本长度,我希望所有文本都显示而不被截断。

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

最佳答案

它是 VGrid,它垂直填充列。在您的情况下,它只有一列。
如果您想在屏幕中显示所有这些内容,则需要增加网格列的数量,例如

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

关于SwiftUI LazyGrid 动态列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62559073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com