How to remove the decimal keyboard of iOS?

I am making an iOS application that requires the user to enter numeric values ​​for multiple text fields. In the UITextField attributes I used

Keyboard = Decimal Panel

Return Key = Done

The keyboard takes up space and hides the submit form button.

In the UIViewController.h file , I wrote the following code for each UITextField, with an event that ended in Exit:

-(IBAction)dismissKeyboard:(id)sender; 

In the UIViewController.m file , I wrote the following code for each UITextField:

 -(IBAction)dismissKeyboard:(id)sender { [textField becomeFirstResponder]; [textField resignFirstResponder]; } 

When I open the iOS simulator for Xcode 5, I do not get any DONE field on the on-screen keyboard. However, if I press the RETURN button on a MacBook keyboard, the on-screen keyboard will disappear.

How can I -

  • Display DONE button on iOS keyboard?
  • Disconnect the keyboard?
+5
source share
5 answers

Since the scroll suggestion has been suggested, I recommend a different approach.

For each numerical input field, set the type of auxiliary input device - toolbar - with the "Finish" button in it. It will look like a panel above the keyboard in Safari. As soon as the user clicks the Finish button, close the keyboard as usual.

This is the simplest solution.

 UIToolbar* toolbar = [UIToolbar new]; UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)]; id space = [UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; toolbar.items = @[space, doneButton]; self.numericTextField1.inputAccessoryView = toolbar; self.numericTextField2.inputAccessoryView = toolbar; ... 
+8
source

In the .h class, first declare the scroll.

 @property (strong, nonatomic) IBOutlet UIScrollView *scrRegistration; 

In class .m use this code

 -(void)viewWillAppear:(BOOL)animated { UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)]; [gesture setNumberOfTapsRequired:1]; [gesture setNumberOfTouchesRequired:1]; [self.scrRegistration addGestureRecognizer:gesture]; [self.scrRegistration setContentSize:CGSizeMake(320, 354)]; for (UIView *vw in [self.scrRegistration subviews]) { if([vw isKindOfClass:[UITextField class]]){ UITextField *txtfld=(UITextField *)vw; txtfld.attributedPlaceholder = [[NSAttributedString alloc] initWithString:txtfld.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:8.0f/255.0f green:94.0f/255.0f blue:165.0f/255.0f alpha:1.0]}]; } } } - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if(textField==DecimalKeyboard) { if([[UIScreen mainScreen] bounds].size.height>480) { [self.scrRegistration setContentOffset:CGPointMake(0, 110) animated:YES]; } else { [self.scrRegistration setContentOffset:CGPointMake(0, 300) animated:YES]; } UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; [numberToolbar sizeToFit]; textField.inputAccessoryView = numberToolbar; } } -(void)hideKeyboard:(UITapGestureRecognizer *)tap { [DecimalKeyboard resignFirstResponder]; [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView commitAnimations]; } -(void)cancelNumberPad { for (UIView *vw in [self.scrRegistration subviews]) { if([vw isKindOfClass:[UITextField class]]) { UITextField *txtfld=(UITextField *)vw; if ([txtfld isFirstResponder] == YES) { [txtfld resignFirstResponder]; } } } [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView commitAnimations]; } -(void)doneWithNumberPad { [DecimalKeyboard resignFirstResponder]; [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView commitAnimations]; } 
+2
source

You might be better off just placing the content on the screen in a scrollview, and then resizing the view when the keyboard is present. This is usually the approach that I take when I have an input form, and it is very easy to implement. To do this, you can follow the instructions in this answer:

fooobar.com/questions/538253 / ...

First, make sure you register your viewController for keyboard notifications (and remove observers when the view goes away):

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

Here is the bulk of the code. It is assumed that you have a view that covers the entire screen. If this is not the case, you need to configure CGRect in two ways below:

 - (void)keyboardWillShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; } - (void)keyboardWillHide:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; } 
+1
source

In case someone comes across this question while learning Swift, after many unsuccessful attempts, I finally had some success with this code in my ViewController, based on Lev Nathan's answer:

 @IBOutlet var decimalField : UITextField! override func viewDidLoad() { super.viewDidLoad() // ... addDoneButtonToKeyboard() // ... // Do any additional setup after loading the view, typically from a nib. refreshUI() } func addDoneButtonToKeyboard() { var doneButton:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "hideKeyboard") var space:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) var items = [AnyObject]() items.append(space) items.append(doneButton) toolbar.items = items decimalField.inputAccessoryView = toolbar } func hideKeyboard() { decimalField.resignFirstResponder() } 
+1
source

You should consider entering a scroll form. Thus, the application can scroll through the form so that the submit button becomes visible. Your users can also scroll up and down to view all of the input.

To have a numeric keypad with the button pressed, you need to create a custom input view.

An alternative is to refuse the keyboard from any keystroke behind the keyboard and text fields, but the user interface will be inconvenient.

0
source

Source: https://habr.com/ru/post/1200487/


All Articles