简体   繁体   English

滚动在 WebView macOS 中不可见

[英]Scroll not visible in WebView macOS

Is it possible to make the scroll not visible in WebView for macOS?是否可以在 macOS 的 WebView 中使滚动不可见?

TO IMPLEMENT THE WEBVIEW实现 WEBVIEW

I have a WebView from the library to the Storyboard I have the connection我有一个 WebView 从图书馆到 Storyboard 我有连接

 @IBOutlet weak var webView1: WKWebView!

I have in the ViewController.swift:我在 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: -iOS 解决方案在 macOS 中不起作用:

webView1.scrollView.isScrollEnabled = false

- There are some questions similar, but different: OS X Swift WebView disable scrolling - 有一些类似但不同的问题: OS X Swift WebView 禁用滚动

He asks how to disable WebView.他询问如何禁用 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'它给了我一个错误:“WKWebView”类型的值没有成员“mainFrame”

It gives me an error: Value of type 'WKWebView' has no member 'mainFrame'它给了我一个错误:“WKWebView”类型的值没有成员“mainFrame”

I guess the mentioned thread refers to the deprecated legacy WebView and not the newer WKWebView.我猜提到的线程是指已弃用的旧版WebView ,而不是较新的 WKWebView。

Is it possible to make the scroll not visible in WebView for macOS?是否可以在 macOS 的 WebView 中使滚动不可见?

As already discussed in this SO thread , there is no property for disabling the scroll functionality.正如在这个SO thread中已经讨论的那样,没有用于禁用滚动功能的属性。 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).但是请参阅 react-native 社区的这个拉取请求,以了解如何通过覆盖 WKWebView class(objective-c)来规避这个问题。

Here is a working example in Swift:这是 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.记住不要在 IBOutlet 中更改 class,还要在 storyboard 中更改。

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.一些Cocoa控件内部有滚动视图,如NSCollectionView等等。 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.这是水平的,您也必须将其设置为垂直。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM