SwiftUI避免键盘挤压文本字段

时间:2020-10-02 21:35:11

标签: swift ios14 swiftui

显示键盘时,视图正在压缩而不是向上移动。它在纵向模式下可以正常工作,问题仅在横向模式下出现。附有照片以供澄清。我已经尝试过.edgesIgnoreSafeArea()以及this thread上介绍的解决方案。

VStack {
                
                Text("\(titleText)")
                    .font(.system(size: 30))
                    .bold()
                
                VStack {
                    Text("Item Title")
                    TextField("Item Title", text: $title)
                        .formStyle()
                    
                }
                
                VStack {
                    Text("Item Description")
                    TextField("Item Description", text: $itemDescription)
                    //TextEditor(text: $itemDescription)
                        .formStyle()
                }
                
                VStack {
                    Text("Unit Type")
                    TextField("Each, Total, Hours, LF, etc...", text: $unitType)
                        .formStyle()
                }
                
                VStack {
                    Text("Unit Cost")
                    TextField("Cost per unit", text: $unitCost, onEditingChanged: { (isChanged) in
                        guard !unitCost.isEmpty else { return }
                        //Ensuring entered text can be converted to required double
                        if let _ = Double(unitCost) {
                            isUnitCostValid = true
                        } else {
                            isUnitCostValid = false
                            unitCost = ""
                            alertTitle = .unitCostError
                            alertMessage = "Not a valid Unit Cost. Please enter only numbers."
                            showingAlert = true
                        }
                    })
                    .keyboardType(.numberPad)
                    .formStyle()
                }
                
                Spacer()
                
                Button("\(saveText)") {
                    //code to print to console coredata path for device
                    // print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
                    addNewItem()
                }
                .font(.system(size: 30))
                .frame(maxWidth: 400, maxHeight: 100)
                .foregroundColor(.white)
                .background(Color.blue)
                .clipShape(Capsule())
                .disabled(isDisabled)
                .opacity(isDisabled ? 0.7 : 1)
                .alert(isPresented: $showingAlert) {
                    Alert(title: Text(alertTitle.rawValue), message: Text(alertMessage), dismissButton: .default(Text("OK"), action: {
                        if case .saved = alertTitle {
                            settingsPopper.popToSettings()
                            //use presentationMode... if not using our custom popper
                            //presentationMode.wrappedValue.dismiss()
                        }
                    }))
                }
            }

有什么想法吗?

Before

After

0 个答案:

没有答案