为什么HStack和LazyHGrid中的SwiftUI按钮间距不正确?

iswrvxsc  于 2022-11-28  发布在  Swift
关注(0)|答案(1)|浏览(190)

我最近在Xcode上开发了一个iPadOS应用,现在已经在应用商店上了。但是,一位用户报告说这个应用有问题。在我的测试过程中,在一些iPad上,它运行良好,比如

然而,使用第八代iPad 2的用户看到的是不同的屏幕,如

这个问题在带有123、Delete、Clear和Space按钮的HStack中尤其明显,在字母网格(LazyGrid)中也是如此。代码:
`

Other Code

var body: some View {
        GeometryReader { g in
            HStack(spacing: 0) {
                switch view {
                case "text":
                Button(action: {
                    view = "number"
                }) {
                    ZStack {
                    Rectangle()
                        .frame(width: g.size.width/4, height: g.size.height/2)
                        .foregroundColor(.white)
                        .border(.gray, width: 1)
                    Text("123")
                        .font(.system(size: g.size.width/30))
                        .foregroundColor(.black)
                    }
                }
                .buttonStyle(.borderless)
                case "number":
                    Button(action: {
                        view = "text"
                    }) {
                        ZStack {
                        Rectangle()
                            .frame(width: g.size.width/4, height: g.size.height/2)
                            .foregroundColor(.white)
                            .border(.gray, width: 1)
                        Text("ABC")
                            .font(.system(size: g.size.width/30))
                            .foregroundColor(.black)
                        }
                    }
                    .buttonStyle(.borderless)
                default:
                    Text("Error")
                }
                Button(action: {
                    text = ""
                }) {
                    ZStack {
                    Rectangle()
                        .frame(width: g.size.width/4, height: g.size.height/2)
                        .foregroundColor(.white)
                        .border(.gray, width: 1)
                    Text("Clear")
                        .font(.system(size: g.size.width/30))
                        .foregroundColor(.black)
                    }
                }
                .buttonStyle(.borderless)
                Button(action: {
                    if text != "" {
                    text.remove(at: text.index(before: text.endIndex))
                                                }
                }) {
                    ZStack {
                    Rectangle()
                        .frame(width: g.size.width/4, height: g.size.height/2)
                        .foregroundColor(.white)
                        .border(.gray, width: 1)
                    Text("Delete")
                        .font(.system(size: g.size.width/30))
                        .foregroundColor(.black)
                    }
                }
                .buttonStyle(.borderless)
                Button(action: {
                    text += " "
                }) {
                    ZStack {
                    Rectangle()
                        .frame(width: g.size.width/4, height: g.size.height/2)
                        .foregroundColor(.white)
                        .border(.gray, width: 1)
                    Text("Space")
                        .font(.system(size: g.size.width/30))
                        .foregroundColor(.black)
                    }
                }
                .buttonStyle(.borderless)
            }
            .offset(y: -g.size.height/2)
        }
    }

Other Code

`
我已经将HStack中的间距设置为0,希望它删除按钮之间的间距,但它对它没有任何影响。

xxb16uws

xxb16uws1#

尝试简化代码并使用堆栈来为您提供优势如何?关键是通过使用.frame(maxWidth:.infinity)来扩展按钮以填充空间,而不是使用默认的行为来适应内容。

struct KeysViews: View {
 var body: some View {
    VStack (alignment:.center,spacing: 1.0) {
        HStack (spacing:1.0) {
            but("123")
            but("Clear")
            but("Space")
        }.frame(maxWidth:.infinity)
        HStack (spacing:1.0) {
            ForEach(["A","B","C","D","E",","],id:\.self) {c in
                but(c)
            }
        }
        HStack (spacing:1.0) {
            ForEach(["F","J","H","I","J","."],id:\.self) {c in
                but(c)
            }
        }
        HStack (spacing:1.0) {
            ForEach(["K","L","M","N","O","!"],id:\.self) {c in
                but(c)
            }
        }
        HStack (spacing:1.0) {
            ForEach(["P","Q","R","S","T","?"],id:\.self) {c in
                but(c)
            }
        }
    }.frame(maxWidth:.infinity).background(.gray).padding()
}

func but(_ text : String) -> some View {
    Button(text, action: {}).frame(maxWidth:.infinity).background(.white)
}
}

第一次

相关问题