在UITextField的.NumberPad键盘上添加一个减号

Ger*_*ski 1 uitextfield ios swift custom-keyboard

相当新的iOS开发,请原谅我提出一些可能非常明显的事情.众所周知,键盘类型设置为.NumberPad的UITextField键盘如下所示......

.NumberPad键盘

我想做的是用减号替换左下角的空白区域.这是可能的,还是需要编写一个完整的自定义键盘来实现这一目标?

真的很感激帮助.

Ole*_*man 8

在文本域inputAccessoryView中添加一个工具栏,当文本字段成为响应者时,键盘将显示工具栏(Swift 3.0):

func addToolBar(){
   let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
   let minusButton = UIBarButtonItem(title: "-", style: .plain, target: self, action: #selector(toggleMinus))
   toolbar.items = [minusButton]
   theTextField.inputAccessoryView = toolbar
}

func toggleMinus(){

    // Get text from text field
    if var text = theTextField.text , text.isEmpty == false{

        // Toggle
        if text.hasPrefix("-") {
            text = text.replacingOccurrences(of: "-", with: "")
        }
        else
        {
            text = "-\(text)"
        }

        // Set text in text field
        theTextField.text = text

    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.