UIWebView Keyboard - Getting rid of the “Previous/Next/Done” bar

后端 未结 9 2086
清酒与你
清酒与你 2020-12-13 07:05

I want to get rid of the bar on top of the keyboard that appears when you focus a text field in a webview. We have some other ways of handling this and it\'s redundant and

相关标签:
9条回答
  • 2020-12-13 08:07
    [[NSNotificationCenter defaultCenter] addObserver:self
    
                                             selector:@selector(keyboardWasShown:)
    
                                                 name:UIKeyboardDidShowNotification object:nil];
    -(void)keyboardWasShown:(NSNotification*)aNotification
    {
    UIWindow* tempWindow;
    
        //Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
        //UIKeyboard is a subclass of UIView anyways
        UIView* keyboard;
    
        //Check each window in our application
        for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
        {
                //Get a reference of the current window
                tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
    
                //Get a reference of the current view
                for(int i = 0; i < [tempWindow.subviews count]; i++)
                {
                        keyboard = [tempWindow.subviews objectAtIndex:i];
    
                        if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
                        {      
                keyboard.hidden = YES;
                UIView* keyboardLayer;
                for(int n = 0; n < [keyboard.subviews count]; n++)
                {
                    keyboardLayer = [keyboard.subviews objectAtIndex:n];
                    NSLog(@" keyboardLayer ::: %@ " ,keyboardLayer);
                    if([[keyboardLayer description] hasPrefix:@"<UIWebFormAccessory"] == YES)
                    {
                        [keyboardLayer removeFromSuperview ];
                    }
                }
                keyboard.hidden = NO;
    
                        }
                }
        }
    
     NSLog(@"keyboardWasShown" );
    
    }
    

    check this as well: http://pastebin.com/s3Fkxvsk

    0 讨论(0)
  • 2020-12-13 08:11

    Not easily. You could try to go poking around the subviews in the web view but it would be taboo with Apple.

    How about not putting the text field in the web page on the web side, and adding your textfield/textview to the webview explicitly so it doesn't show the nav bar at all, and you can add your own from scratch?

    0 讨论(0)
  • 2020-12-13 08:12

    this code definetly works for me... hope this also works for you.

    - (void)viewDidLoad{
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    
    -(void)viewWillAppear:(BOOL)animated{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification {
        [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 *possibleFormView in [keyboardWindow subviews]) {
            if ([[possibleFormView description] hasPrefix:@"<UIPeripheralHostView"]) {
                for (UIView* peripheralView in [possibleFormView subviews]) {
    
                    // hides the backdrop (iOS 7)
                    if ([[peripheralView description] hasPrefix:@"<UIKBInputBackdropView"]) {
                        //skip the keyboard background....hide only the toolbar background
                        if ([peripheralView frame].origin.y == 0){
                            [[peripheralView layer] setOpacity:0.0];
                        }
                    }
                    // hides the accessory bar
                    if ([[peripheralView description] hasPrefix:@"<UIWebFormAccessory"]) {
                        // remove the extra scroll space for the form accessory bar
                        UIScrollView *webScroll;
                        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
                            webScroll = [[self webviewpot] scrollView];
                        } else {
                            webScroll = [[[self webviewpot] subviews] lastObject];
                        }
                        CGRect newFrame = webScroll.frame;
                        newFrame.size.height += peripheralView.frame.size.height;
                        webScroll.frame = newFrame;
    
                        // remove the form accessory bar
                        [peripheralView removeFromSuperview];
                    }
                    // hides the thin grey line used to adorn the bar (iOS 6)
                    if ([[peripheralView description] hasPrefix:@"<UIImageView"]) {
                        [[peripheralView layer] setOpacity:0.0];
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题