0

I have a textfield inside UIWebview. When pressing done button, keyboard not hiding and continue to display over the view. I want to dismiss the keyboard whenever done button is pressed. Is there any way to get done button action in my swift code?

Any help will be appreciated.

Jan
  • 1,744
  • 3
  • 23
  • 38

1 Answers1

2

by textfield inside a UIWebview , I take it as UITextField as a subview inside a UIWebview right ? in that s the case you just need to implement the UITextFieldDelegate in your class, specifically this delegate method textFieldShouldReturn , which is called whenever the done button is tapped, so basically you ll have something like this :

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
       if (textField == correctTextField) {
           textField.resignFirstResponder()
        }
        return true
    }

obviously you will need to assign that controller as the textField delegate in the first place

but supposedly it s not a native textfield but rather a html or some kind of a part of the webcontent then what i would do , i would make it invoke a request with a special scheme, for exemple "hideKeyboard" and i would implement uiwebviewdelegate and test for that scheme to hide the keyboard whenever i encouter it, here s an exemple in obj c:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *URL = [request URL];
    NSString *scheme = [URL scheme];
    if ([scheme isEqualToString:@"hideKeyboard"]) {
      [self.view endEditing:true];
    }
}
Hussein Dimessi
  • 377
  • 3
  • 8
  • textfield is not native, its created from webpage. – Jan Jun 21 '17 at 09:10
  • ok that s more precise, well in that case the question becomes what does the done button invoke in that "webpage"? – Hussein Dimessi Jun 21 '17 at 09:17
  • I just want to hide keyboard when hit return button, in webpage cant do anything related to that. – Jan Jun 21 '17 at 11:26
  • no i meant does it send a request or something ...? that way way you can implement the webview delegate , intercept that request and hide the keyboard then – Hussein Dimessi Jun 21 '17 at 11:46
  • so for every url i have to check and hide keyboard right? Actually many more search bar are there in the webpage so it will be a difficult task – Jan Jun 21 '17 at 12:07
  • nope here s what i would do, make a special scheme and test on it, for exemple instead of http ... i ll use a custom scheme "hideKeyboard" .. and test on schemes for it in the delegate method, i ll put in exemple in my original comment – Hussein Dimessi Jun 21 '17 at 12:09