Force the keyboard to dismiss on iPad when using MonoTouch

I have been using monotouch for a while and am now coming up against common iOS problems that I had during the days of using Objective-C.

One of these is the problem of the Keyboard not dismissing when ResignFirstResponder is called when the View controller is presented modally on iPad

So to get round this I managed to port over the old trick of using the UIKeyboardImpl class to dismiss it.

This is an extension class to UIView that loads a new UIKeyboardImpl instance, finds the active keyboard and hides it.

using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;

namespace SomeProject
{
    public static class UIKitExtensions
    {

        public static void HideKeyboard(this UIView vc)
        {
            var keyb = new NSObject(MonoTouch.ObjCRuntime.Class.GetHandle ("UIKeyboardImpl"));
            var activeinstance = Messaging.intptr_objc_msgSend(keyb.Handle, new Selector("activeInstance").Handle);
            
            Messaging.void_objc_msgSend(activeinstance,new Selector("dismissKeyboard").Handle);
        }

    }
}


Enjoy!

Dave

Day: 12,724


Popular posts from this blog

First Blog Posting Wierdness