Thursday, March 15, 2012

Hiding the inputAccessoryView of a UIWebView

UIWebView is a workhorse of a view. You can use it to display web pages, of course, but you can also use it to display locally-generated rich content.

I was recently using UIWebView and the contentEditable attribute to implement a text field that allows inline images, etc. The fact that I'm using a web view, though, is an implementation detail; to the user, it should just look like any other text field.

Unfortunately, UIWebView loves to show that little "Previous/Next" bar above the keyboard. It was useless and occupying valuable screen space, so I determined to find a way around it.

This bar is the enemy
It was actually fairly simple; a UIWebView contains a UIWebBrowserView, a private class that does most of the real work. I discovered via class-dump that it also implements -(id)inputAccessoryView, returning the bar in question. So at runtime, I create a subclass of UIWebBrowserView and override that method to return nil. Then I call [self reloadInputViews], and I'm done.

I've wrapped this up into a UIWebView category, which exposes a single property:
@property (nonatomic, assign) BOOL hackishlyHidesInputAccessoryView;
You can find it on gist.


Note: This code has only been tested on iOS 5.1, and may not work on other versions of iOS. I've filed rdar://11040111 requesting a supported way to hide this bar. For now, though, this works, and it should fail gracefully if the internal UIWebView view hierarchy changes. If Apple ever releases a supported method for hiding this bar, please use that instead. I only resort to hackery like this when I can find no other option.