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:
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:
Unless I am missing something major here, this would likely mean one of two things:
- There should be a scrollView but there isn't (i.e. it was accidentally deleted by Apple???)
- 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
Update Nstouchbar on the Fly to Add/Remove Items Programmatically
Operation Not Permitted When Executing 'Killall' with Swift
Generics and Functional Programming in Swift
Why Do Two Distinct Array Literals Equal Each Other in Swift
How to Prompt for Accessibility Features in a MACos App (From the Appdelegate)
Simple Observable Struct with Rxswift
What Are the Fundamental Differences Between Set and Didset
Swiftui - How to Change Text Alignment of Label in Toggle
Enum Not Working in Custom Initializer
How to Get Core Data Entity by It's Objectid
Swift Alamofire Add Custom Header to All Requests
Handling an Attribute of an Xml Element in Swift
Perform Migration by Adding List() and Another Model Class
Can Not Cast Value of Type Nstaggedpointerstring to Nsdictionary
Guard Let Error: Initializer for Conditional Binding Must Have Optional Type Not 'String'
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files