Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss keyboard when using DecimalPad

I have a UITableView with a custom cell that has a TextField. I have the DecimalPad comes up, and as we all know, there is no done key. I previously had resolved this type of issue when I had a "Decimal only" textfield on a normal UIView by handling the TouchesEnded event and then checking to see if the TextField was the first responder and if so, it would then resign, but if that technique could work now then I'm not able to figure out who's TouchesEnded I should be using (The UIView that everything is presented on, the UITableView, the Cell, the CellControler, the TextField.. I think I've tried everything).

I'm hoping there's another, cleaner way of dealing with this.

Anyone?

like image 590
Driss Zouak Avatar asked Apr 03 '11 23:04

Driss Zouak


People also ask

How do you dismiss a keyboard?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do I dismiss a keyboard in Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

How do I hide the TextField keyboard?

When you click on the TextField it opens up the on-screen keyboard. To hide/dismiss the keyboard you have to press the back button in Android and the done button (inside the onscreen keyboard) in iOS.

How do I close the keyboard on iOS 15?

If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.


1 Answers

I think David has the best idea - here is some Monotouch code to get you started. You will need to put this in the View Controller where the decimal pad is being shown:

UIView dismiss;

public override UIView InputAccessoryView 
{
    get 
    {
        if (dismiss == null)
        {
            dismiss = new UIView(new RectangleF(0,0,320,27));
            dismiss.BackgroundColor = UIColor.FromPatternImage(new UIImage("Images/accessoryBG.png"));          
            UIButton dismissBtn = new UIButton(new RectangleF(255, 2, 58, 23));
            dismissBtn.SetBackgroundImage(new UIImage("Images/dismissKeyboard.png"), UIControlState.Normal);        
            dismissBtn.TouchDown += delegate {
                 textField.ResignFirstResponder();
            };
            dismiss.AddSubview(dismissBtn);
        }
        return dismiss;
    }
}
like image 153
Luke Avatar answered Oct 19 '22 23:10

Luke