XCode SwiftUI - 为什么我的小键盘工具栏会这样?

XCode SwiftUI - Why is my keypad toolbar doing this?

非常 应用程序开发游戏的新手。我试图将此工具栏放在 .decimalPad 上方,但我无法消除这个大差距。

        VStack {
            Rectangle()
                .foregroundColor(Color(UIColor.systemBackground))
                .frame(height: 35)
                .overlay {
                    HStack {
                        Spacer()
                        Button(action: {
                            isTextFieldFocused = false
                            
                        }) { Text("Done")}
                    }
                    .offset(y: -3)
                    .padding(.trailing)
                }
                .opacity(isTextFieldFocused ? 1 : 0)
                .ignoresSafeArea(.keyboard) //This makes sure the bottom tab bar stays below the keyboard.
        }

我最初认为是另一个视图中的某些东西导致了间距,但我设法解析了 canvas 中的视图并且无论如何它都这样做了。

这是我想要的样子,供参考。 What I want

要在键盘上添加 Button,请使用 .toolbar.keyboard 的位置,如下所示:

    TextField("Enter Text", text: $text)
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Button(action: {
                     isTextFieldFocused = false
                }) { Text("Done")}
                // If you want it leading, then use a Spacer() after
                Spacer()
            }
        }

您添加 Rectangle 是想多了。这就是我们寻找最小可重现示例的原因。我们可以针对您的特定代码拨打修复程序。