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

Asynchronous Request with NSOperationQueue

Today post is about how to run a asynchronous task in NSOperationQueue.  Generally we do not run a Asynchronous task in NSOperationQueue. It is also not recommended for any programmer to do that. This post is only for learning purpose what will happen if we schedule a asynchronous task in a queue and how can we complete that task:). So let us move to the learning: NSOperationQueue: In iOS NSOperationQueue is a class, which provide a way to perform operation concurrently. We also have others way to perform concurrent operation: 1) GCD 2) NSThread 3) pThread NSOperationQueue is a wrapper on GCD, which provides a very convenient way to execute operation concurrently. To create a Queue for Operation you have to simply allocate a object of the class: NSOperationQueue * opertionQueue = [[ NSOperationQueue alloc ] init ]; For this post let suppose you are making a queue to handle all Http request in your application. So i want to create a queue in Handl...

NSThread with Asynchronous Request

NSThread: It is class used in Cocoa framework to create a thread.You have to specify a target and selector to create a new thread in objective c. So the selector or method is main entry point of your thread.  Detail: What happen once the thread execute all the code in the method (Thread main routine)? Thread is terminated after execute all the code in the entry(its main routine) routine. So how we can run an asynchronous request on NSThread ? What is Async request? It is non blocking request which permit the application to do other task side by side as request is in continue. So it enhance parallelism in the application. Now question is how we can prepare a asynchronous request in iOS: - ( void ) startAsyncRequestWithUrl:( NSString *)urlString{     assert (urlString != nil && urlString. length > 0 );     NSURL * url = [[ NSURL alloc ] initWithString :urlString];     NSURLRequest * urlRequest = [[ NSURLReq...

Everything about BLE with iOS Part1 (Introduction)

In this article we will learn how to turn your iPhone to behave as peripheral (who has data to send) or central (who receive the data). What is BLE (Bluetooth Low energy device) ? Bluetooth LE is a wireless personal area network technology like its previous incarnation, Classic Bluetooth. It can be use to transfer and receiving of the data. What is difference between Classic and LE ? BLE has low power consumption and low data transfer rate. BLE can support large number of slaves. On the other hand Classic (3.0) support maximum 7 devices connectivity. Unique feature of BLE is advertising  functionality,  So other devices can scan,  connect and  receive data from the advertising BLE device. Other difference is in classic you need to pair the devices first for the communication. Terms: Peripheral:  It is device which advertise the service. In client-server architecture form we can say peripheral is...