Scroll not visible in WebView macOS

帅比萌擦擦* 提交于 2020-07-10 10:29:26

问题


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'


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/62246942/scroll-not-visible-in-webview-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!