Nrc
Nrc

Reputation: 9787

Scroll not visible in WebView macOS

Is it possible to make the scroll not visible in WebView for macOS?

TO IMPLEMENT THE WEBVIEW

I have a WebView from the library to the Storyboard I have the connection

 @IBOutlet weak var webView1: WKWebView!

I have in the ViewController.swift:

webView1.load(URLRequest(url: URL(string: "https:www.apple.com")!))

WHAT I HAVE TRIED

I have tried many things but it could be helpful to comment:

-The iOS solution do not work in macOS:

webView1.scrollView.isScrollEnabled = false

- There are some questions similar, but different: OS X Swift WebView disable scrolling

He asks how to disable WebView. I ask how to make it not visible. In fact, I do not mind if it is disabled or not.

In any case, I have tried that solution:

webView1.mainFrame.frameView.allowsScrolling = false;

It gives me an error: Value of type 'WKWebView' has no member 'mainFrame'

Upvotes: 0

Views: 1337

Answers (2)

eemmrrkk
eemmrrkk

Reputation: 1700

webview.enclosingScrollView?.verticalScrollElasticity = .allowed
webview.enclosingScrollView?.horizontalScrollElasticity = .allowed
webview.enclosingScrollView?.scrollerKnobStyle = .dark // change it

Some of Cocoa controls has scrollView inside of itself like NSCollectionView and many more. The lines of above makes give you allow to scroll enabled and it gives scroll visible because of scroll has enable and it has some style.

And if you want to hide scroll indicator you can use;

webview.enclosingScrollView?.horizontalScroller?.isHidden = true

This is for horizontal, you must set it with vertical too.

Upvotes: 1

Yannic
Yannic

Reputation: 817

It gives me an error: Value of type 'WKWebView' has no member 'mainFrame'

I guess the mentioned thread refers to the deprecated legacy WebView and not the newer WKWebView.

Is it possible to make the scroll not visible in WebView for macOS?

As already discussed in this SO thread, there is no property for disabling the scroll functionality. But see this pull request from the react-native community to get an idea of how to circumvent this by overriding the WKWebView class (objective-c).

Here is a working example in Swift:

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

Remember to change the class not even in the IBOutlet, but also in the storyboard.

Upvotes: 2

Related Questions