gpt4 book ai didi

ios - 向 UITextField 的 .NumberPad 键盘添加减号

转载 作者:行者123 更新时间:2023-11-30 13:00:21 26 4
gpt4 key购买 nike

对 iOS 开发相当陌生,所以请原谅我问一些可能非常明显的问题。众所周知,UITextField 的键盘(keyboardType 设置为 .NumberPad)如下所示...

.NumberPad keyboard

我想做的是将左下角的空白区域替换为减号。这是否可能,或者是否需要编写整个自定义键盘才能实现这一目标?

非常感谢您的帮助。

最佳答案

将工具栏添加到文本字段 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

}
}

希望有帮助。

关于ios - 向 UITextField 的 .NumberPad 键盘添加减号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com