首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ios键盘上添加额外的按键

如何在ios键盘上添加额外的按键
EN

Stack Overflow用户
提问于 2020-10-07 11:14:35
回答 2查看 108关注 0票数 1

在termius中,当您打开键盘时,键盘顶部会有额外的按键,如图所示。

我如何在swift中做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-07 11:49:59

只需将按钮添加到字段:

代码语言:javascript
复制
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

或者另一个:

代码语言:javascript
复制
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 = button

SwiftUI解决方案:

代码语言:javascript
复制
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()
    }

}
票数 1
EN

Stack Overflow用户

发布于 2020-10-07 11:54:21

您可以将UIToolbar与您想要的任何按钮一起使用,只需将该工具栏添加到输入文本字段的inputAccessoryView中。就像下面的代码。

代码语言:javascript
复制
let bar = UIToolbar()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self,action:#selector(doneTapped))
bar.items = [doneButton]
bar.sizeToFit()
inputTextField.inputAccessoryView = bar
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64236681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档