Skip to main content

iOS8 UIWebView Remove or Modify Keyboard Toolbar

Remove Toolbar:


One of my client requirements is to remove the default toolbar for the keyboard. I found numerous examples for iOS7 to remove toolbar from the keyboard window. But it did not work for iOS8. So i came to following solution after some Research on Google:


Step1:  

First we need to add observer for keyboard notification:

[[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(removeKeyboardTopBar:) name:UIKeyboardWillShowNotification object:nil];

Step 2:

We need to implement the method for observer:

- (void)removeKeyboardTopBar {
   
}

Step 3:

Find the keyboard window from all the application windows:

 - (void)removeKeyboardTopBar {
   UIWindow *keyboardWindow = nil;
    UIView* toolBarContainer = nil;
    NSArray* windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *possibleWindow in windows) {
        if (![[possibleWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = possibleWindow;
            break;
        }
    }
}

Step 4:

Find and remove the toolbar:

iOS 7 :

for (UIView *possibleFormView in [keyboardWindow subviews]){
       if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound){
                for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
                    if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                        [subviewWhichIsPossibleFormView removeFromSuperview];
                    }
                }
    }
}

iOS 8:

In iOS 8 you cannot remove the toolbar due to some constraint invalidation issue. So you can hide the view as in following example:


for (UIView *possibleFormView in [keyboardWindow subviews]){
         if([[possibleFormView description] hasPrefix:@"<UIInputSetContainerView"]) {
                for(int i = 0 ; i < [possibleFormView.subviews count] ; i++)
                {
                    UIView* hostkeyboard = [possibleFormView.subviews objectAtIndex:i];
                    if([[hostkeyboard description] hasPrefix:@"<UIInputSetHostView"])
                    {
                        for (id temp in hostkeyboard.subviews)
                        {
                            if ([[temp description] hasPrefix:@"<UIWebFormAccessory"])
                            {
                                UIView* currentToolBar = (UIView*)temp;
                                currentToolBar.hidden = true; //hide the view
                                
                            }
                        }
                    }
             }
     }
}

We are done with tool bar remove code. Following is full source code for remove the tool bar and add a new one in place of that. It will work for both the version:

Source Code:

- (void) removeKeyboardTopBar:(NSNotification*)notify{
    // Locate non-UIWindow.
    
    UIWindow *keyboardWindow = nil;
    UIView* toolBarContainer = nil;
    NSArray* windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *possibleWindow in windows) {
        if (![[possibleWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = possibleWindow;
            break;
        }
    }
    CGRect frm = keyboardWindow.frame;
    CGRect toolbarFrame = CGRectMake(0.0f, frm.size.height, frm.size.width, 44.0f);
    // Locate UIWebFormView.
    for (UIView *possibleFormView in [keyboardWindow subviews])
    {
        if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
            if([[possibleFormView description] hasPrefix:@"<UIInputSetContainerView"])
            {
                for(int i = 0 ; i < [possibleFormView.subviews count] ; i++)
                {
                    UIView* hostkeyboard = [possibleFormView.subviews objectAtIndex:i];
                    if([[hostkeyboard description] hasPrefix:@"<UIInputSetHostView"])
                    {
                        for (id temp in hostkeyboard.subviews)
                        {
                            if ([[temp description] hasPrefix:@"<UIWebFormAccessory"])
                            {
                                UIView* currentToolBar = (UIView*)temp;
                                currentToolBar.hidden = true;
                                toolbarFrame = currentToolBar.frame;
                                toolBarContainer = hostkeyboard;
                            }
                        }
                    }
                }
            }
        }else{
            if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
                for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
                    if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                        [subviewWhichIsPossibleFormView removeFromSuperview];
                    }
                }
            }
        }
        
    }
    UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
    if(toolBarContainer){
        [toolBarContainer addSubview:toolBar];
    }else{
        [keyboardWindow addSubview:toolBar];
        
        [UIView animateWithDuration:0.25 animations:^{
            toolBar.frame = CGRectMake(0.0f, 220.0f, toolbarFrame.size.width, toolbarFrame.size.height);
        }];
        
        toolBar.frame = CGRectMake(0.0f, 0.0f, 320.0, 44.0f);
        
        UIView *v = [[keyboardWindow subviews] objectAtIndex:0];
        [v addSubview:toolBar];
    }
    
    
    keyboardWindow = nil;
}

Comments

Popular posts from this blog

Implement orientation modes in iPhone Hybrid Applications

Let suppose you are working on a hybrid application which runs only in single (portrait) mode. One day a requirement come that PDF and Doc Viewer (HTML Page) should support both (landscape, portrait) mode. Your Application loads all the HTML content from the local html files and you need to implement the above functionality only for one HTML file. Let break the above task in the modules: Step 1: Application should detect when the PDF and Doc viewer is open in application. I setup location.href tag in html to " docvieweron:// " and " docvieweroff:// " when page is open and closed respectively. In this way I am getting a delegate callback in web view: WebViewDelegate: - ( BOOL ) webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request   navigationType: ( UIWebViewNavigationType )navigationType {          NSString * urlString = [[request URL ] absoluteString ];...

Deep Diving Objective-C Properties (Part 1)

Properties Properties in objective c are a convenient way to provide the accessors (getter and setter) for instance variable in objective - c language. Let suppose you have a mobile then what are its properties? Name: Brand Name: Screen Size: etc. So properties define the things. In a similar manner properties in objective - c define the class or objects. A Simple Example for a Person class with the basic attribute. Example:  @interface  Person :  NSObject @property   NSString * name; @end Compiler will generate 
a getter and setter for you with the following convention: - ( void ) setName:( NSString *)name{     //Compiler generated the code for getter property } - ( NSString *)name{         //Compiler generated the code for getter property } From the above code there are some questions raised in mind? 1) W...