Dimiss keyboard when editing textfield on UIWebview

帅比萌擦擦* 提交于 2019-12-10 11:01:18

问题


I'm opening a web page on UIWebview and this page has a textfield and a virtual keyboard the to enter password. But when edit the textfield iPhone virtual keyboard appears as well. I wanted to dismiss iPhone keyboard and on the same time continue editing the textfield. I have tried the following but it did not work.

[webView stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];

And also tried to register for Keyboard notification so I can dimiss the keyboard but did not succeed. I would appreciate any ideas about how to achieve this. Thanks in advance.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector (keyboardDidShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];



-(void) keyboardDidShow: (NSNotification *)notif 
{
// I want to dismiss the keyboard 
}

回答1:


try this

[webView endEditing:YES]

You also can try this link if above solution does not work - Hide keyboard in UIwebView




回答2:


Add method that go over all the webview subviews and resign the first responder

     -(BOOL)resignFirstResponderAction:(UIView *)view{
        if (view.isFirstResponder){
            [view resignFirstResponder];
            return YES;
        }
        
        for (UIView *subView in view.subviews) {
            if ([self resignFirstResponderAction:subView]){
                return YES;
            }
        }
        return NO;
     }

then just call this method with the webview:

     [self resignFirstResponderAction:webView];

Edit

it seems there is much simpler solution:

[webView endEditing:YES]

I leave the original answer as it might help in some cases




回答3:


You can use javascript to take the focus away from the html element when it gets clicked like this:

 <input type="text" onClick="document.getElementById('myInput').blur();" id="myInput"/>



回答4:


You need to add next category of UIView to your code
And the method above to your implementation Add the next code to your .m file, before the @implementation

@implementation UIView (FindFirstResponder)

- (UIView *)findFirstResponder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];

        if (firstResponder != nil) {
            return firstResponder;
        }
    }

    return nil;
}
@end


-(void)hideWebViewFirstResponser{
    UIView *firstResponderOwner=[webView findFirstResponder];
    if(firstResponderOwner) {
        [firstResponderOwner resignFirstResponder];
    }
}


来源:https://stackoverflow.com/questions/9108877/dimiss-keyboard-when-editing-textfield-on-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!