gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:29 27 4
gpt4 key购买 nike

对 iOS 开发还很陌生,所以请原谅我问了一些可能很明显的问题。众所周知,将 keyboardType 设置为 .NumberPad 的 UITextField 键盘看起来如下所示...

.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/36455158/

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