首页 > 解决方案 > WKWebView in Cocoa app can't get the cursor on click

问题描述

I created a simple test app where one field is an NSTextField and another is a <textarea> instead a WKWebView. I can click into the web view once, but if I go back to the NSTextField, I'm unable to get the cursor back into the web view ever again.

Here's a demonstration:

enter image description here

As you can see, the web view is still interactive to some extent, it just can't become (I assume) the first responder again.

Is there something weird about WKWebView and the responder chain? How can I make the WebWebView behave like a normal NSTextField?

Here is a sample project built in Xcode 11.5 in case you want to try it out.

How can this be fixed?

标签: cocoawkwebviewappkitnstextfieldnsresponder

解决方案


The issue seems to be related to the textarea, using content editable <body contenteditable="true"></body> does seem to work.

A workaround for using the textarea is to subclass WKWebView like this:

- (void)mouseDown:(NSEvent *)event
{
    [super mouseDown:event];
    [[self window] makeFirstResponder:self];
}

推荐阅读