Forum Xamarin.iOS

iOS Newb - How do you get rid of the Keyboard?

RobVRobV USMember

I'm trying to get up to speed with iOS. I create a small app that takes text and displays it after clicking a button. (simplified description)
After I've clicked into a textbox and enter some text, I would like to close (get rid of) the on screen keyboard but I am unable to find a gesture to do just that. What am I missing? Rob

Posts

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RobV,

    Are you referring to manually closing it, or programatically closing it?

    To manually close it on the iPhone,

    Touch the Done, Return, Go, etc button at the lower right of the keyboard. The Text of that button changes depending on the functionality of it. In the attached screen shot, its the Go button.

    For the iPad, touch the button in the lower right of the keyboard, which Dismisses it. In the attached image, it the button that has what looks like a keyboard with an arrow below it.

    To programmatically dismiss the keyboard, call ResignFirstResponder() on the control like:

     myTextView.ResignFirstResponder();
    
  • RobVRobV USMember

    I have a View with 2 textboxes and a button.


    
 public override void ViewDidLoad ()
 {
 base.ViewDidLoad ();
 
 btnVC02.TouchUpInside += (object sender, EventArgs e) => { 
 string firstName = tbxFirstName.Text; 
 string lastName = tbxLastName.Text;
 RVClass01 rv = new RVClass01();
 lblVC02.Text = rv.MakeFullName(firstName, lastName);
 } ;
 }



    Once focus is set to a textbox, the keyboard won't exit. Also, there is no "Done" or "Go" button on the Keyboard.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RobV,

    Can you attach a screenshot of what you see?

  • RobVRobV USMember

    I've attached a screenshot

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RobV,

    I apologize, I think you have to add something like this:

    myTextField.ShouldReturn = delegate
    {
        text.ResignFirstResponder();
        return true;
    };
    
  • RobVRobV USMember

    Where would that go? (Sorry, new to Xamarin and iOS)

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            btnVC02.TouchUpInside += (object sender, EventArgs e) => { 
                string firstName = tbxFirstName.Text; 
                string lastName = tbxLastName.Text;
                RVClass01 rv = new RVClass01();
                lblVC02.Text = rv.MakeFullName(firstName, lastName);
            };
        }
    
  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai
    edited July 2013

    @RobV

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        btnVC02.TouchUpInside += (object sender, EventArgs e) => { 
            string firstName = tbxFirstName.Text; 
            string lastName = tbxLastName.Text;
            RVClass01 rv = new RVClass01();
            lblVC02.Text = rv.MakeFullName(firstName, lastName);
        };
    
        tbxFirstName.ShouldReturn = delegate
        {
            // Changed this slightly to move the text entry to the next field.
            tbxLastName.BecomeFirstResponder();
            return true;
        };
    
        tbxLastName.ShouldReturn = delegate
        {
            tbxLastName.ResignFirstResponder();
            return true;
        };
    }
    
  • RobVRobV USMember

    Hey John! Thanks for sticking with me!

    I tried your code but the keyboard is still stuck on screen...

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RobV,

    Thats odd, I just made a project and it works fine for me...

    Do you have a test project you can upload and share?

  • RobVRobV USMember

    Here's a Zip from my Mac

  • RobVRobV USMember
    edited July 2013

    What's more... This works... But I can't weave it into my code

    http://docs.xamarin.com/recipes/ios/input/keyboards/dismiss_the_keyboard

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RobV,

    I just opened your solution and it works fine.

    I navigated to Tab2, touched the First Name entry area, entered a name, pressed Return, it moved down to last name, entered a name, press return and the keyboard dismissed.

    I am not sure if I am missing what you are talking about, but its working fine using your project you uploaded.

  • RobVRobV USMember

    I see... you have to hit "return" which also means... "Hide Keyboard"... Thanks for your help :>

  • GeorgePrinsGeorgePrins ZAUniversity

    I have a similar problem, when I test my app in the simulator, the return key works great, but when I test it on the device it simply wont fire...?

  • kunalthakkarkunalthakkar INMember ✭✭

    You can use the follwing code for hiding keyboard :

    this.txtFieldname.ShouldReturn += (textField) => {
    textField.ResignFirstResponder();
    return true;
    };

  • GeorgePrinsGeorgePrins ZAUniversity

    I have that exact same code in my ViewDidLoad()...and still it does nothing. To be honest, none of the changes that I make to my ViewController.cs class reflects on my device, but does on the simulator... I just dont know why...

  • kunalthakkarkunalthakkar INMember ✭✭

    @George Prins : I tried it, working for me on both simulator and device. you should create new solution and try it again.

  • jameszhoujameszhou CNMember ✭✭

    the code from @AlexandrePalma below works great:

    var g = new UITapGestureRecognizer(() => View.EndEditing(true));
    View.AddGestureRecognizer(g);
    

    but except one thing when targeting on iOS5, any button Touch event like TouchUpInside on the view will not be fired (see http://stackoverflow.com/questions/13515539/uibutton-not-works-in-ios-5-x-everything-is-fine-in-ios-6-x)

    and the easiest solution I found is as per this http://stackoverflow.com/questions/3344341/uibutton-inside-a-view-that-has-a-uitapgesturerecognizer

    var g = new UITapGestureRecognizer(() => View.EndEditing(true));
    g.CancelsTouchesInView = false; //for iOS5
    View.AddGestureRecognizer(g);
    
  • nicholasaltnicholasalt USMember

    @jameszhou‌

    It works, Thank you man!~

  • PalaniSBalaPalaniSBala USMember

    @johnmiller.1234, hi i am new to this platform.. can u explain how to define text field in delegate and how to access it in viewdidload?(for RobV's example code u mentioned delegate.)

  • MarcialVegaMarcialVega CLMember

    i use this form, maybe is not correct but work for me :)

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
    if(MyEditText.IsFirstResponder && this.View !=MyEditText)
    MyEditText.ResignFirstResponder();

    base.TouchesBegan(touches,evt);
    

    }

  • TripleDoubleTripleDouble BGMember ✭✭✭

    @jameszhou

    Thank you

  • ThamaraiThamarai Member ✭✭
    edited January 2018

    @AlexPalma , It works. This is what I want.... Thank you for saving my time.

    @jameszhou ,

    I added touchdown event. So far no problem. Anyway, thank you for additional information.

Sign In or Register to comment.