Iain Delaney
Iain Delaney

Reputation: 562

How can I get the Edit Menu to appear in a UIWebView with contentEditable?

I have an iPad App with an embedded Text Editor. The editor is a UIWebView with contenteditable set to true. The form also contains a simple text field for the title of the document.

If the user types in the title, then taps Return they can enter text in WebView, and tap-and-hold to get the magnifying glass and then the edit menu.

If, however, the user enters the title, and then taps on the web view they can enter text, tap-and-hold to get the magnifying glass, but the edit menu never appears.

The only difference I can see is that in the first case the textFieldShouldReturn: method fires first, and then the textFieldDidEndEditing: method fires, while in the second case only the textFieldDidEndEditing: method fires.

Here are the two methods in question:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

and

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *js = [NSString stringWithFormat:@"document.getElementById('theBody').setAttribute('contenteditable','true')"];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
    [self.webView becomeFirstResponder];
    [self.webView stringByEvaluatingJavaScriptFromString: @"document.getElementsByTagName('body')[0].focus()"];
}

Does anyone have any idea what the difference is, and how I can get the edit menu to appear?

Upvotes: 2

Views: 255

Answers (1)

Omer
Omer

Reputation: 5590

I believe this is an iOS 7 bug, try adding the following code to your view controller as a workaround.

- (void)viewDidLoad {
    [super viewDidLoad];
     self.edgesForExtendedLayout = UIRectEdgeNone;
     //Additional inicialization code.
}

Note that property is available on iOS 7 or later. Hope this helps!

Upvotes: 2

Related Questions