【问题标题】:Why is the toolbar not showing when I tap on the textfield?为什么当我点击文本字段时工具栏不显示?
【发布时间】:2020-05-30 18:09:37
【问题描述】:

我想在点击文本字段时在数字键盘上方实现一个完成的工具栏,但是由于某种原因工具栏没有显示。

已使用以下代码示例:

extension UITextField{

    @IBInspectable var doneAccessory: Bool{
        get{
            return self.doneAccessory
        }
        set (hasDone) {
            if hasDone{
                addDoneButtonOnKeyboard()
            }
        }
    }

    func addDoneButtonOnKeyboard()
    {
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        doneToolbar.barStyle = .default

        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))

        let items = [flexSpace, done]
        doneToolbar.items = items
        doneToolbar.sizeToFit()

        self.inputAccessoryView = doneToolbar
    }

    @objc func doneButtonAction()
    {
        self.resignFirstResponder()
    }
}

【问题讨论】:

  • 检查UIScreen.main.bounds.width这个函数addDoneButtonOnKeyboard()是否大于0?
  • 它的显示非常好

标签: ios swift toolbar inputaccessoryview


【解决方案1】:

确保在您的故事板中,TextField 的属性检查器 doneAccessory 属性设置为 ON

O/P 看起来像

【讨论】:

  • 谢谢。这就是问题所在。
  • @joco: 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多