Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIMenuController Editing Menu in UIWebView only appears second time

I am finding that in my app in iOS 9, when I long press in a UIWebView, the editing menu that normally appears, only appears the second (and following) times that I long press. The first time I long press in any particular webview no menu at all shows, although the selection handles do appear. The second time I long press in any partiular webview, it behaves as expected.

Another strange symptom is that occasionally (but not every time) as the menu appears, something quickly animates sliding of towards the left of the screen, originating from the menu, but it is too fast to see what it is. It happens even when the edit menu appears as it should. I expect this is related to the issue.

I have only tested in the simulator because I haven't installed iOS 9 on a device yet.

In iOS 8 the same menu works as expected.

Another developer gave me a work around, which may work when you have only one UIWebVeiw on screen. Put this in viewDidAppear (thanks Chen Xian'an):

[[myWebView.scrollView.subviews firstObject]   becomeFirstResponder];  
[[NSOperationQueue mainQueue] addOperationWithBlock:^{  
    [self becomeFirstResponder];  
}];  

However, since I have more than one webView it is not an ideal solution for me. And if I apply it to each webview it only works on the last one I apply it to.

This appears to be a bug, and hopefully Apple will fix it soon, in the meantime, does anyone have any bright ideas as to how I could make this work for more than one webView?

like image 783
narco Avatar asked Sep 20 '15 23:09

narco


2 Answers

I tried in this way, and I succeeded. First You must custom UIWebView and write

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Second You must call this methods before handle your long press

[yourWebView becomeFirstResponder];

I hope it would help..

like image 151
iGuan7u Avatar answered Nov 01 '22 04:11

iGuan7u


In my case the problem that was the UIWindow where the UIWebView was apparently not the key window. What i did to solve it was to add a call to [webView.window makeKeyAndVisible] at the webViewDidFinishLoad method.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Fix
    [webView.window makeKeyAndVisible];
}
like image 2
Leo137 Avatar answered Nov 01 '22 03:11

Leo137