Skip to content

Instantly share code, notes, and snippets.

@kgaidis
Last active June 23, 2019 18:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgaidis/5f9a8c7063b687cc3946fad6379c1a66 to your computer and use it in GitHub Desktop.
Save kgaidis/5f9a8c7063b687cc3946fad6379c1a66 to your computer and use it in GitHub Desktop.
UIWebView does not allow to customize the `inputAccessoryView` without using magic. Just simply change the `customInputAccessoryView` property to switch to another view. Inspiration from: https://gist.github.com/bjhomer/2048571
#import <UIKit/UIKit.h>
@interface UIWebView (CustomInputAccessoryView)
@property (strong, nonatomic) UIView *customInputAccessoryView;
@end
#import "UIWebView+CustomAccessory.h"
#import <objc/runtime.h>
@implementation UIWebView (CustomInputAccessoryView)
static const char * const kCustomInputAccessoryViewClassString = "UIWebBrowserViewCustomAccessoryView";
static Class kCustomInputAccessoryViewClass = Nil;
#pragma mark - Accessors
- (void)setCustomInputAccessoryView:(UIView *)customInputAccessoryView
{
objc_setAssociatedObject([self browserView], @selector(browserViewCustomInputAccessoryView), customInputAccessoryView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self loadCustomInputAccessoryView];
}
- (UIView *)customInputAccessoryView
{
return objc_getAssociatedObject([self browserView], @selector(customInputAccessoryView));
}
#pragma mark - Browser View Custom Method
- (UIView *)browserViewCustomInputAccessoryView
{
return objc_getAssociatedObject(self, @selector(browserViewCustomInputAccessoryView));
}
#pragma mark - Helpers
- (void)loadCustomInputAccessoryView
{
UIView *browserView = [self browserView];
// Register a new class if needed
if (!kCustomInputAccessoryViewClass) {
Class newClass = objc_allocateClassPair([browserView class], kCustomInputAccessoryViewClassString, 0);
IMP newImplementation = [self methodForSelector:@selector(browserViewCustomInputAccessoryView)];
class_addMethod(newClass, @selector(inputAccessoryView), newImplementation, "@@:");
objc_registerClassPair(newClass);
kCustomInputAccessoryViewClass = newClass;
}
// Switch to the new class if we haven't already
if ([self class] != kCustomInputAccessoryViewClass) {
object_setClass(browserView, kCustomInputAccessoryViewClass);
}
[browserView reloadInputViews];
}
- (UIView *)browserView
{
UIScrollView *scrollView = self.scrollView;
UIView *browserView = nil;
for (UIView *subview in scrollView.subviews) {
if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
browserView = subview;
break;
}
}
return browserView;
}
@end
@novellizator
Copy link

Can I only edit the view, meaning to keep the previous/next add new buttons? I believe, there must be some NotificationCenter notification the next/previous buttons are conveying. Do you happen to know them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment