【问题标题】:Add return key or Done button to `phonePad` keyboard type of TextField in SwiftUI在 SwiftUI 中为 TextField 的“phonePad”键盘类型添加返回键或完成按钮
【发布时间】:2020-11-27 15:23:37
【问题描述】:

在 SwiftUI 中,普通键盘带有返回键,可用于关闭键盘。但是,对于特定的键盘类型,例如 phonePad,没有返回键。

我们可以使用一些技巧来添加附件视图,但是,是否有任何 SwiftUI 方法来处理这个?

这是我的代码

TextField("Phone Number", text: .constant("12345"))
                    .keyboardType(.phonePad)
                    .textContentType(.telephoneNumber)

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    没有简单的方法,但 IMO 最好的选择是添加一个工具栏,如图所示 here:

    import SwiftUI
    
    struct ContentView: View {
        @State private var phoneNumber = ""
        var body: some View {
            NavigationView {
                KeyboardView {
                    HStack {
                        Spacer()
                        TextField("Phone Number", text: $phoneNumber)
                            .padding()
                            .overlay(RoundedRectangle(cornerRadius: 8)
                            .stroke(Color.secondary, lineWidth: 1)
                            .frame(height: 50))
                            .keyboardType(.phonePad)
                        Spacer()
                    }
                } toolBar: {
                    HStack {
                        Spacer()
                        Button {
                            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                        } label: { Text("Done") }
                    }.padding()
                }
            }
        }
    }
    

    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    class KeyboardResponder: ObservableObject {
        private var notificationCenter: NotificationCenter
        @Published private(set) var height: CGFloat = .zero
        init(center: NotificationCenter = .default) {
            notificationCenter = center
            notificationCenter.addObserver(self, selector: #selector(keyBoardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
            notificationCenter.addObserver(self, selector: #selector(keyBoardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
        }
        func dismiss() {
            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
        deinit { notificationCenter.removeObserver(self) }
        @objc func keyBoardWillShow(_ notification: Notification) {
            height = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? .zero
        }
        @objc func keyBoardWillHide(_ notification: Notification) {
            height = .zero
        }
    }
    

    struct KeyboardView<Content, ToolBar>: View where Content: View, ToolBar: View {
        @StateObject private var keyboard = KeyboardResponder()
        let toolbarFrame = CGSize(width: UIScreen.main.bounds.width, height: 40)
        var content: () -> Content
        var toolBar: () -> ToolBar
        var body: some View {
            ZStack {
                content().padding(.bottom, keyboard.height == .zero ? .zero : toolbarFrame.height)
                VStack {
                     Spacer()
                     toolBar()
                        .frame(width: toolbarFrame.width, height: toolbarFrame.height)
                        .background(Color.secondary)
                }
                .opacity(keyboard.height == .zero ? .zero : 1)
                .animation(.easeOut)
            }
            .padding(.bottom, keyboard.height)
            .edgesIgnoringSafeArea(.bottom)
            .animation(.easeOut)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-05
      • 2012-09-11
      • 1970-01-01
      • 2023-03-30
      • 2023-03-15
      • 2012-04-22
      • 2016-03-19
      • 2011-05-20
      相关资源
      最近更新 更多