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

相当新的iOS开发,所以原谅我问一些可能是相当明显的东西。 正如你所知道的键盘types设置为.NumberPad的UITextField的键盘看起来像下面…

.NumberPad键盘

我想要做的是用一个减号replace左下angular的空白空间。 这是可能的或者是否需要编写一个完整的自定义键盘来实现这一目标?

真的很感激帮助。

添加一个工具栏到你的文本框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 } } 

希望能帮助到你。