在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?
我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?
当前回答
另一个解决方案。如果屏幕上有其他非数字填充文本字段,则完美。
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
其他回答
另一个解决方案。如果屏幕上有其他非数字填充文本字段,则完美。
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
以下是对Luda的回答的全面检查,并进行了以下更改:
附件视图会自动调整到应用程序框架的宽度 避免使用已弃用的常量UIBarButtonItemStyleBordered Done按钮被实例化为UIBarButtonSystemItemDone
目前“Done”按钮位于附件视图的中央。您可以通过删除相关一侧的空格将其定位在左侧或右侧。
我省略了“取消”按钮,因为默认键盘也没有这个按钮。如果你确实想要一个“取消”按钮,我建议你实例化它为一个UIBarButtonSystemItemCancel,并确保你没有丢弃文本字段中的原始值。在Luda的回答中实现的“取消”行为会用空字符串覆盖值,这可能不是你想要的。
- (void)viewDidLoad {
[super viewDidLoad];
float appWidth = CGRectGetWidth([UIScreen mainScreen].applicationFrame);
UIToolbar *accessoryView = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, appWidth, 0.1 * appWidth)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(selectDoneButton)];
accessoryView.items = @[space, done, space];
self.valueField.inputAccessoryView = accessoryView;
}
- (void)selectDoneButton {
[self.valueField resignFirstResponder];
}
有关构建附件视图的更多信息,请参阅Apple关于数据输入自定义视图的文档。你可能会想要参考UIToolbar和UIBarButtonItem的参考页面。
最简单的方法是:创建自定义透明按钮并将其放置在左下角,它将具有与UIKeyboardTypeNumberPad中的空白空间相同的CGSize。切换(显示/隐藏)这个按钮在textField成为firstresponder,按钮点击分别。
Swift 2.2 /我用了Dx_的答案。然而,我希望所有的键盘都有这个功能。所以在我的基类中,我把代码:
func addDoneButtonForTextFields(views: [UIView]) {
for view in views {
if let textField = view as? UITextField {
let doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(dismissKeyboard))
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
textField.inputAccessoryView = doneToolbar
} else {
addDoneButtonForTextFields(view.subviews)
}
}
}
func dismissKeyboard() {
dismissKeyboardForTextFields(self.view.subviews)
}
func dismissKeyboardForTextFields(views: [UIView]) {
for view in views {
if let textField = view as? UITextField {
textField.resignFirstResponder()
} else {
dismissKeyboardForTextFields(view.subviews)
}
}
}
然后只需调用addDoneButtonForTextFields在self.view.subviews在viewDidLoad(或willDisplayCell如果使用表格视图)添加完成按钮到所有键盘。
如果你有多个数字字段,我建议子类化UITextField来创建一个NumericTextField,它总是显示一个带有done按钮的数字键盘。然后,简单地将你的数值字段与接口构建器中的这个类关联起来,你就不需要在任何视图控制器中添加任何额外的代码。下面是我在Xcode 8.0中使用的Swift 3.0类。
class NumericTextField: UITextField {
let numericKbdToolbar = UIToolbar()
// MARK: Initilization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}
// Sets up the input accessory view with a Done button that closes the keyboard
func initialize()
{
self.keyboardType = UIKeyboardType.numberPad
numericKbdToolbar.barStyle = UIBarStyle.default
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let callback = #selector(NumericTextField.finishedEditing)
let donebutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: callback)
numericKbdToolbar.setItems([space, donebutton], animated: false)
numericKbdToolbar.sizeToFit()
self.inputAccessoryView = numericKbdToolbar
}
// MARK: On Finished Editing Function
func finishedEditing()
{
self.resignFirstResponder()
}
}
斯威夫特4.2
class NumericTextField: UITextField {
let numericKbdToolbar = UIToolbar()
// MARK: Initilization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}
// Sets up the input accessory view with a Done button that closes the keyboard
func initialize()
{
self.keyboardType = UIKeyboardType.numberPad
numericKbdToolbar.barStyle = UIBarStyle.default
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let callback = #selector(NumericTextField.finishedEditing)
let donebutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: callback)
numericKbdToolbar.setItems([space, donebutton], animated: false)
numericKbdToolbar.sizeToFit()
self.inputAccessoryView = numericKbdToolbar
}
// MARK: On Finished Editing Function
@objc func finishedEditing()
{
self.resignFirstResponder()
}
}