ios - LazyVGrid 中的 SwiftUI 中心项目

标签 ios swift xcode swiftui

我是 SwiftUI 的新手,正在尝试将 LazyVGrid 中的元素居中看法。
这是我目前所拥有的:
enter image description here
使用:

var body: some View {
    ZStack {
        Color(red: 75/255, green: 0, blue: 130/255).ignoresSafeArea()
        VStack {
            LazyVGrid(columns: [GridItem(.adaptive(minimum: 30))], alignment: .center, spacing: 10) {
                let letters = wordToArray(word: testWord)
                ForEach(Array(letters.enumerated()), id: \.offset) { letter in
                    Text(String(letter.element).capitalized)
                        .foregroundColor(.blue)
                        .frame(width:30, height: 40)
                        .background(Color.yellow)
                        .cornerRadius(5)
                }
            }
            .padding()
        }
    }
}
但是正如您所看到的,这只会将元素向左对齐 - 我也不知道可能需要显示的字符数。我还希望保持它们之间的间距与上述相同。
任何帮助将不胜感激!
谢谢。

最佳答案

2个选项

struct CenteredLVSView: View {
    @State var letters: [String] = ["A", "B", "C", "D", "E","A", "B", "C", "D", "E","A", "B", "C", "D", "E"]
    //Specify column count and it will justify/center by width
    @State var columnCount: Int = 5
    var body: some View {
        ZStack {
            Color(red: 75/255, green: 0, blue: 130/255).ignoresSafeArea()
            LazyVGrid(columns: Array(repeating: GridItem(.flexible(minimum: 30
                 //If you set max it will center on width if smaller than max, uncomment below
                 //, maximum: 40
            )), count: columnCount), spacing: 10){
                ForEach(Array(letters.enumerated()), id: \.offset) { letter in
                    Text(String(letter.element).capitalized)
                        .foregroundColor(.blue)
                        .frame(width:30, height: 40)
                        .background(Color.yellow)
                        .cornerRadius(5)
                }
            }
            .padding()
        }
    }
}
LazyVGrid - Justified
更改为 LazyHGrid
struct CenteredLVSView: View {
    @State var letters: [String] = ["A", "B", "C", "D", "E","A", "B", "C", "D", "E","A", "B", "C", "D", "E"]
    //Specify row count and it will justify/center height
    @State var rowCount: Int = 5
    var body: some View {
        ZStack {
            Color(red: 75/255, green: 0, blue: 130/255).ignoresSafeArea()
            LazyHGrid(rows: Array(repeating: GridItem(.flexible(minimum: 30
                //If you set max it will center if smaller than max, height uncomment below
                //, maximum: 40
            )), count: rowCount), spacing: 10){
                ForEach(Array(letters.enumerated()), id: \.offset) { letter in
                    Text(String(letter.element).capitalized)
                        .foregroundColor(.blue)
                        .frame(width:30, height: 40)
                        .background(Color.yellow)
                        .cornerRadius(5)
                }
            }
            .padding()
        }
    }
}
LazyHGrid - Justified
正如评论中提到的,如果您设置了 maximum他们将居中/合理化到那个最大值
LazyHGrid w/3 行,最多 40
LazyHGrid w/ 3 rows
LazyVGrid 有 5 列,最多 40
LazyVGrid w/ 5 columns

关于ios - LazyVGrid 中的 SwiftUI 中心项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68385250/

相关文章:

iphone - 将其添加到项目时无法将任何新的 .H 文件添加到目标,

ios - 导航栏与 iphone 8 模拟器的 swift xcode 中的状态栏发生碰撞

ios - 如何解决 linker command failed with exit code 1 的错误(使用 -v 查看调用)

iphone - 触摸开始 :(NSSet *)touches withEvent:(UIEvent *)event doesn't get called

ios - requestalwaysauthorization() 突然消失

ios - 保存 UIButton 的状态(复选框)- Swift

ios - 无法使用 Dropbox API for Swift 创建共享链接

ios - 我们能否以编程方式一次更改多个 UILabel 的字体(通过最少的代码行)?

ios - 处理 iPad Mini 屏幕尺寸

ios - 如何使用PromiseKit/Photos?