Show keyboard in contenteditable UIWebView programmatically

心不动则不痛 提交于 2019-11-29 09:55:06

问题


My UIWebView loads a contenteditable html content. I want to let the UIWebView get focused and show keyboard in the UIWebView.

I use [self.webview becomeFirstResponder], but it doesn't work. Is there any work around method?

Thanks.


回答1:


UIwebview is not editable and you can not make it first responder. Where as you can use javascripts for that purpose , try out this answer

UIWebView with contentEditable (html editing), first responder handling?




回答2:


This is now possible in iOS 6. See the boolean keyboardDisplayRequiresUserAction in UIWebView. Once set to NO then you can use focus() to set the cursor.




回答3:


In iOS 6 > you need to 1) set your webview to keyboardDisplayRequiresUserAction = NO, and 2) call focus. Here's the code:

self.webView.keyboardDisplayRequiresUserAction = NO; // put in viewDidLoad

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        [self.webView
         stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].focus()"];
    return YES;
}

You can use document.getElementById('someId').focus() as an alternative. I've set the textField as the delegate and used the delegate method textFieldShouldReturn which fires when the return key is pressed.




回答4:


need set "Visible at Launch" checkbox in you window object - keyboard will show automaticaly on input field



来源:https://stackoverflow.com/questions/9835956/show-keyboard-in-contenteditable-uiwebview-programmatically

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