How to hide keyboardAccessory bar in Xamarin.iOS

What is the keyboardAccessory bar?
As the image below, when user focus on an input in a webview, the keyboard will be launched with a keyboardAccessory bar. User can click “Done” to hide the keyboard.

Screen Shot 2015-09-25 at 5.01.19 pm

Sometime we don’t want it appears, we can also remove it in our codes.
In iOS, there is a library can do this

But in Xamarin.iOS, we can just use these codes. It is simple and easily to use.

    public class HideFormAccessoryBar

        extern static IntPtr class_getInstanceMethod(IntPtr classHandle, IntPtr Selector);

        extern static IntPtr method_getImplementation(IntPtr method);

        extern static IntPtr imp_implementationWithBlock(ref BlockLiteral block);

        extern static void method_setImplementation(IntPtr method, IntPtr imp);

        static IntPtr UIOriginalImp;
        static IntPtr WKOriginalImp;
        static bool _hideFormAccessoryBar;
        public static void SetHideFormAccessoryBar(bool hide)
            if (hide == _hideFormAccessoryBar)
            var uiMethod = class_getInstanceMethod(Class.GetHandle("UIWebBrowserView"), new Selector("inputAccessoryView").Handle);
            var wkMethod = class_getInstanceMethod(Class.GetHandle("WKContentView"), new Selector("inputAccessoryView").Handle);

                UIOriginalImp = method_getImplementation(uiMethod);
                WKOriginalImp = method_getImplementation(wkMethod);

                var block_value = new BlockLiteral();
                CaptureDelegate d = MyCapture;
                block_value.SetupBlock(d, null);
                var nilimp = imp_implementationWithBlock(ref block_value);
                method_setImplementation(uiMethod, nilimp);
                method_setImplementation(wkMethod, nilimp);

                method_setImplementation(uiMethod, UIOriginalImp);
                method_setImplementation(wkMethod, WKOriginalImp);
            _hideFormAccessoryBar = hide;

        delegate IntPtr CaptureDelegate();

        static IntPtr MyCapture()
            return IntPtr.Zero;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s