有没有类似数字键盘但允许输入负数的UITextField键盘类型?

22
我在我的UITextFields上将UIKeyboardType设置为UIKeyboardTypeNumberPad。但是,这只允许用户输入正整数。我需要用户能够输入负数或正数。是否有一个与UIKeyboardTypeNumberPad完全相同但包括“-”(减号)符号的UIKeyboardType?如果没有,我该如何创建它?
谢谢。

创建您自定义的键盘 - Sport
1
其他选项是,在inputAccessoryView中添加一个(-)负号按钮。或者满足于:UIKeyboardTypeNumbersAndPunctuation - Onik IV
@OnikIV 如何将减号按钮添加为inputAccessoryView? - AndyW
@user3210941 代码在回答中。 - Onik IV
3个回答

24
在Swift中有一个带有+/-按钮的版本,还有一个清除和完成按钮,可以用于数字键盘。 数字键盘截图
extension UITextField {

func addNumericAccessory(addPlusMinus: Bool) {
    let numberToolbar = UIToolbar()
    numberToolbar.barStyle = UIBarStyle.default

    var accessories : [UIBarButtonItem] = []

    if addPlusMinus {
        accessories.append(UIBarButtonItem(title: "+/-", style: UIBarButtonItem.Style.plain, target: self, action: #selector(plusMinusPressed)))
        accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding after
    }

    accessories.append(UIBarButtonItem(title: "Clear", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadClear)))
    accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding space
    accessories.append(UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadDone)))

    numberToolbar.items = accessories
    numberToolbar.sizeToFit()

    inputAccessoryView = numberToolbar
}

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

@objc func numberPadClear() {
    self.text = ""
}

@objc func plusMinusPressed() {
    guard let currentText = self.text else {
        return
    }
    if currentText.hasPrefix("-") {
        let offsetIndex = currentText.index(currentText.startIndex, offsetBy: 1)
        let substring = currentText[offsetIndex...]  //remove first character
        self.text = String(substring)
    }
    else {
        self.text = "-" + currentText
    }
}

}

17

虽然有点烦人,但并没有这样的系统键盘可用。除了创建自定义键盘之外,最好的选择是使用UIKeyboardTypeNumbersAndPunctuation,并进行验证以强制执行有效的数字输入。


1
UIKeyboardTypeDecimalPad似乎没有减号按钮。为了整洁,我只想要必需的按键,所以必须研究创建自定义按钮。 - AndyW
抱歉,我实际上是想说UIKeyboardTypeNumbersAndPunctuation,但复制/粘贴了错误的内容。已编辑答案。 - Clafou
在这种情况下,那实际上是我现在正在做的事情,但是用所有标点符号看起来有点糟糕,你甚至可以在这个键盘上切换到字母表。 - AndyW
我知道,不太好看!我很想知道苹果的理由是什么。顺便说一句,即使UIKeyboardTypeNumbersAndPunctuation也有问题,因为它只适用于iPhone,如果在iPad上使用它,你会得到与UIKeyboardTypeNumbersAndPunctuation相同的结果。 - Clafou
这样吗?到目前为止,我只在iPhone上进行了测试,这很糟糕。谢谢。 - AndyW
不管怎样都需要进行验证以处理用户粘贴的文本。 - lostintranslation

5

添加减号作为accessoryView,你需要将你的textField作为属性,例如在这个例子中,这个属性是:myTextField。

在创建myTextField后,你需要添加以下代码,例如在viewDidLoad中:

      UIView *inputAccesoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
// It´s good idea a view under the button in order to change the color...more custom option
inputAccesoryView.backgroundColor = [UIColor whiteColor];

UIButton *minusButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-150)/2, 5, 150, 30)];
// configure the button here... you choose.
[minusButton setTitle:@"-" forState:UIControlStateNormal];
[minusButton addTarget:self action:@selector(changeNumberSing) forControlEvents:UIControlEventTouchUpInside];
[inputAccesoryView addSubview:minusButton];

self.myTextField.inputAccessoryView = inputAccesoryView;

在您的viewController中添加此按钮的方法:

-(void)changeNumberSing
{
if ([self.myTextField.text hasPrefix:@"-"])
{
    self.myTextField.text = [self.myTextField.text substringFromIndex:1];
}else
{
    self.myTextField.text = [NSString stringWithFormat:@"-%@",self.myTextField.text];
}
}

这个可行!只需要将按钮标题颜色设置为黑色即可看到文本。但是这在键盘上方,是否可能将其放置在键盘的左下角瓷砖上(对于此键盘类型来说,这是空白区域)? - AndyW
不可以。你可以在键盘上方绘制(例如更改按钮的“y”坐标),但它不会接收任何事件。 - Onik IV
那很烦人。不过还是谢谢你的帮助! - AndyW
@AndyW 可能值得查看“设置为黑色”与新的暗模式交互的方式。 - Shayne
@Shayne 谢谢,不过最终我还是使用了另一个答案。 - AndyW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接