iOS keyboard style in webview?

2020-03-31 06:54发布

问题:

I am pretty new to web development, my project is using Jquery-mobile, Phonegap and compass (scss).

By default, the keyboard showing up when an input field gets focus, is a keyboard containing a sort of top bar with fields navigation abilities. I would like to get rid of this navigation bar, and display the simplest keyboard available in iOS. I tried enclosing my objects in a form or without a form tag, without success. I don't have any clue how to achieve this, nore if it is possible :/

Thx for any advice!

If you hit this problem, make sure to head over to https://bugreport.apple.com and duplicate rdar://9844216

回答1:

To remove the bar you should dive into the objective-c code.

This is the code that I use: (inside the controller that contains your UIWebview add the following code)

First add an observer to receive the notification for the keyboard :

-(void)viewWillAppear:(BOOL)animated{
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(keyboardWillShow:)
                                                name:UIKeyboardWillShowNotification
                                              object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {
    [self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
  // Locate non-UIWindow.
  UIWindow *keyboardWindow = nil;
  for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
        keyboardWindow = testWindow;
        break;
    }
  }

  // Locate UIWebFormView.
  for (UIView *formView in [keyboardWindow subviews]) {
    // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
    if ([[formView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
        for (UIView *subView in [formView subviews]) {
            if ([[subView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                // remove the input accessory view
                [subView removeFromSuperview];
            }
            else if([[subView description] rangeOfString:@"UIImageView"].location != NSNotFound){
                // remove the line above the input accessory view (changing the frame)
                [subView setFrame:CGRectZero];
            }
        }
      }
    }
}


回答2:

I was working on iOS 7.0 and fixed it with the following code on UIWebView

-(void)removeExtraBar
{
    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }
    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];
                }
                else if([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIKBInputBackdropView"].location != NSNotFound) {
                    [subviewWhichIsPossibleFormView removeFromSuperview];
                }
            }
        }
    }
}

This works for me with the UIWebView keyboard issue on iOS 7.0