Disable Bounce Scrolling for Wkwebview in MACos

Disable bounce scrolling for WKWebview in macOS

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:

Sample Image

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.

Disable horizontal scrolling for WKWebView

For this, you may use Coordinator. There is good explanation for their.

Create class Coordinator in your UIViewRepresentable. Add UIScrollViewDelegate to class. In makeUIView, set webView?.scrollView.delegate = context.coordinator.

In Coordinator, you need this function.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.x > 0){
scrollView.contentOffset = CGPoint(x: 0, y: scrollView.contentOffset.y)
}
}

And now, horizontal scroll not work!

All code

import WebKit

struct WebView: UIViewRepresentable {
let request: URLRequest
var webView: WKWebView?

class Coordinator: NSObject, UIScrollViewDelegate {
var parent: WebView

init(_ parent: WebView) {
self.parent = parent
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.x > 0){
scrollView.contentOffset = CGPoint(x: 0, y: scrollView.contentOffset.y)
}
}
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

init (request: URLRequest) {
self.webView = WKWebView()
self.request = request
webView?.scrollView.showsHorizontalScrollIndicator = false
webView?.scrollView.showsVerticalScrollIndicator = false

webView?.scrollView.pinchGestureRecognizer?.isEnabled = false
webView?.scrollView.bounces = false
}

func makeUIView(context: Context) -> WKWebView {
webView?.scrollView.delegate = context.coordinator
return webView!
}

func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}

// You funcs
}

Disable rubber-band scrolling for webview in lion

Maybe this article would help you.

In short: disable overflow on HTML and BODY, add a wrapper with overflow:auto around all the page contents

Prevent iOS bounce without disabling scroll ability

This code should stop the bounce as it's the HTML tag that bounces

html {
height : 100%;
overflow: hidden;
position: relative;
}
body {
height : 100%;
overflow: auto;
position: relative;
}


Related Topics



Leave a reply



Submit