首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有像数字键盘但允许负数的UITextField键盘类型?

有没有像数字键盘但允许负数的UITextField键盘类型?
EN

Stack Overflow用户
提问于 2014-12-03 20:45:51
回答 3查看 10.7K关注 0票数 19

我正在将UITextFields上的UIKeyboardType设置为UIKeyboardTypeNumberPad。但是,这只允许用户输入正整数。我需要用户能够输入负整数或正整数。是否有一个与UIKeyboardTypeNumberPad相同但还包含"-“(减号)符号的UIKeyboardType,如果没有,有没有方法可以创建它?

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-03 20:50:11

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

票数 17
EN

Stack Overflow用户

发布于 2019-09-11 22:18:25

这是Swift的一个版本,它有一个+/-按钮,还有一个用于数字键盘的Clear和Done按钮。

numeric keyboard screenshot

代码语言:javascript
复制
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
    }
}

}
票数 18
EN

Stack Overflow用户

发布于 2014-12-03 22:40:15

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

在创建myTextField后,您需要添加此代码,b.e.在viewDidLoad中:

代码语言:javascript
复制
      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中添加此按钮的方法:

代码语言:javascript
复制
-(void)changeNumberSing
{
if ([self.myTextField.text hasPrefix:@"-"])
{
    self.myTextField.text = [self.myTextField.text substringFromIndex:1];
}else
{
    self.myTextField.text = [NSString stringWithFormat:@"-%@",self.myTextField.text];
}
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27272121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档