在termius中,当您打开键盘时,键盘顶部会有额外的按键,如图所示。
我如何在swift中做到这一点?

发布于 2020-10-07 11:49:59
只需将按钮添加到字段:
let keyboardToolbar = UIToolbar()
keyboardToolbar.sizeToFit()
keyboardToolbar.isTranslucent = false
keyboardToolbar.barTintColor = UIColor.white
let addButton = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(someFunction)
)
addButton.tintColor = UIColor.black
keyboardToolbar.items = [addButton]
textView.inputAccessoryView = keyboardToolbar或者另一个:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 60))
button.backgroundColor = UIColor.blue
button.setTitle("NEXT", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(self. yourButton), for: .touchUpInside)
textField.inputAccessoryView = buttonSwiftUI解决方案:
struct TestTextfield: UIViewRepresentable {
@Binding var text: String
var keyType: UIKeyboardType
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = keyType
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
toolBar.items = [doneButton]
toolBar.setItems([doneButton], animated: true)
textfield.inputAccessoryView = toolBar
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
}
extension UITextField{
@objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
self.resignFirstResponder()
}
}发布于 2020-10-07 11:54:21
您可以将UIToolbar与您想要的任何按钮一起使用,只需将该工具栏添加到输入文本字段的inputAccessoryView中。就像下面的代码。
let bar = UIToolbar()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self,action:#selector(doneTapped))
bar.items = [doneButton]
bar.sizeToFit()
inputTextField.inputAccessoryView = barhttps://stackoverflow.com/questions/64236681
复制相似问题