Disable bounce scrolling for WKWebview in macOS

Trying to disable bounce scrolling for WKWebview in macOS, but WKWebview in macOS has no "scrollView"

let webView = WKWebView(frame: view.frame)
print(webView.scrollView)

Solution 1:

One really weird thing about this that you may or may not have noticed is that in the WKWebView class, there is a description of the scrollView that is theoretically "supposed" to be in the class, but the scrollView is not there. This can be seen in the photo below:

macOS WKWebView

I would say that it could be a weird procedure of Apple's, but see in the iOS class, the same description is present but with an actual scrollView this time:

enter image description here

Unless I am missing something major here, this would likely mean one of two things:

  1. There should be a scrollView but there isn't (i.e. it was accidentally deleted by Apple???)
  2. The scrollView was removed from the macOS WKWebView, but the description was accidentally left in the class?

Either way this is a very weird finding.

You might want to consider doing some further research on this, and perhaps filing a Swift bug report.

EDIT:

It was pointed out to me that this scroll view is in-fact a iOS only property, and the unnecessary comment is the bug.

Solution 2:

Scrolling can be disabled by overriding the WKWebView class:

class NoScrollWebView: WKWebView {
    override func scrollWheel(with theEvent: NSEvent) {
        nextResponder?.scrollWheel(with: theEvent)
        return
    }
}

See here: https://stackoverflow.com/a/62746859/9519322