Search code examples
iosswiftscrolltouch

How to remove scrolling inside iOS from the web view of swift5?


I am currently doing a webview. My problem is that I want to prevent iOS from scrolling inside. The scrolling I speak is not the scrolling created in the Web view, but the bouncing scrolling that occurs when you touch up or up by touching down.

It doesn't work for me.

override func loadView() {
        super.loadView()

        WKWebView.scrollView.bounces = false

        URLCache.shared.removeAllCachedResponses()
        URLCache.shared.diskCapacity = 0
        URLCache.shared.memoryCapacity = 0

...
class WebViewController: ... UIScrollViewDelegate {
override func loadView() {
        super.loadView()

        mainWebView.scrollView.bounces = false
        mainWebView.scrollView.delegate = self
...
 func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
            scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
        }
    }

I'm using WKWebView

Thanks you in advance.


Solution

  • This problem was where the function was executed. This function does not work when executed in the LoadView function. You must run it in the ViewDidLoad function.

    override func viewDidLoad() {
            super.viewDidLoad()
    
            WKWebView.scrollView.bounces = false