Last active
June 23, 2019 18:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface UIWebView (CustomInputAccessoryView) | |
@property (strong, nonatomic) UIView *customInputAccessoryView; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?