Reputation: 33
UIWebview
keyboard contains next,previous buttons like (<,>). I need to add a custom toolBar forUIWebView
KeyBoard toolbar. Please advise me. I customised in iOS 8 but it is not working for iOS 7.
- (void) addCustomToolBarForWebViewKeyBoardToolBar
{
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual : [UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([[possibleFormView description] hasPrefix : @"<UIInputSetContainerView"]) {
for (UIView* peripheralView in possibleFormView.subviews) {
for (UIView* peripheralView_sub in peripheralView.subviews) {
if ([[peripheralView_sub description] hasPrefix : @"<UIKBInputBackdropView"] && peripheralView_sub.frame.size.height == 44) {
[[peripheralView_sub layer] setOpacity : 0.0];
}
// hides the accessory bar
if ([[peripheralView_sub description] hasPrefix : @"<UIWebFormAccessory"]) {
UIToolbar *toolBar = [self findWebKeyboardToolbar:peripheralView_sub];
if (toolBar){
editableToolBar=toolBar;
[self checkSelection:self];
return;
}
}
}
}
}
}
}
- (UIToolbar *)findWebKeyboardToolbar:(UIView *)parent
{
if ([parent isKindOfClass:[UIToolbar class]]) {
// the stock toolbar contains a single item with a UISegmentedControl customView.
UIToolbar *tb = (UIToolbar *)parent;
return tb;
}
for (UIView *view in parent.subviews) {
UIToolbar *tb = [self findWebKeyboardToolbar:view];
if (tb) return tb;
}
return nil;
}
Upvotes: 0
Views: 812
Reputation: 122
Please use this category class it will help you.
@interface UIWebView (GUIFixes)
/**
* @brief The custom input accessory view.
*/
@property (nonatomic, strong, readwrite) UIView* customInputAccessoryView;
/**
* @brief Wether the UIWebView will use the fixes provided by this category or not.
*/
@property (nonatomic, assign, readwrite) BOOL usesGUIFixes;
@end
#import "UIWebView+GUIFixes.h"
#import <objc/runtime.h>
@implementation UIWebView (GUIFixes)
static const char* const kCustomInputAccessoryView = "kCustomInputAccessoryView";
static const char* const fixedClassName = "UIWebBrowserViewMinusAccessoryView";
static Class fixClass = Nil;
- (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;
}
- (id)methodReturningCustomInputAccessoryView
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
UIView* view = [self performSelector:@selector(originalInputAccessoryView) withObject:nil];
#pragma clang diagnostic pop
if (view) {
UIView* parentWebView = self.superview;
while (parentWebView && ![parentWebView isKindOfClass:[UIWebView class]])
{
parentWebView = parentWebView.superview;
}
view = [(UIWebView*)parentWebView customInputAccessoryView];
}
return view;
}
- (void)ensureFixedSubclassExistsOfBrowserViewClass:(Class)browserViewClass
{
if (!fixClass) {
Class newClass = objc_allocateClassPair(browserViewClass, fixedClassName, 0);
IMP oldImp = class_getMethodImplementation(browserViewClass, @selector(inputAccessoryView));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
class_addMethod(newClass, @selector(originalInputAccessoryView), oldImp, "@@:");
#pragma clang diagnostic pop
IMP newImp = [self methodForSelector:@selector(methodReturningCustomInputAccessoryView)];
class_addMethod(newClass, @selector(inputAccessoryView), newImp, "@@:");
objc_registerClassPair(newClass);
fixClass = newClass;
}
}
- (BOOL)usesGUIFixes
{
UIView *browserView = [self browserView];
return [browserView class] == fixClass;
}
- (void)setUsesGUIFixes:(BOOL)value
{
UIView *browserView = [self browserView];
if (browserView == nil) {
return;
}
[self ensureFixedSubclassExistsOfBrowserViewClass:[browserView class]];
if (value) {
object_setClass(browserView, fixClass);
}
else {
Class normalClass = objc_getClass("UIWebBrowserView");
object_setClass(browserView, normalClass);
}
[browserView reloadInputViews];
}
- (UIView*)customInputAccessoryView
{
return objc_getAssociatedObject(self, kCustomInputAccessoryView);
}
- (void)setCustomInputAccessoryView:(UIView*)view
{
objc_setAssociatedObject(self,
kCustomInputAccessoryView,
view,
OBJC_ASSOCIATION_RETAIN);
}
@end
Upvotes: 0
Reputation: 1031
iOS 7.x didn't have a UIWebFormAccessory class ,so you can't find it,just replace your code with follow - (void)addCustomToolBarForWebViewKeyBoardToolBar { UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in[[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
UIToolbar *toolBar = [self findWebKeyboardToolbar:keyboardWindow];
toolBar.backgroundColor = [UIColor redColor];
}
- (UIToolbar *)findWebKeyboardToolbar:(UIView *)parent {
if ([parent isKindOfClass:[UIToolbar class]]) {
// the stock toolbar contains a single item with a UISegmentedControl customView.
UIToolbar *tb = (UIToolbar *)parent;
return tb;
}
for (UIView *view in parent.subviews) {
UIToolbar *tb = [self findWebKeyboardToolbar:view];
if (tb) return tb;
}
return nil;
}
Upvotes: 1