9

I am running a WkWebView which is rendering my web content in my app. However when I am scrolling at the top/bottom of the page (vertically) the page bounces like a normal web page and I am losing the native User experience.

I have done some research on this topic which apperantly is a pretty common issue But non of the answeres resolved my problem.

What most people suggest is to use the following: wkWebView.scrollView.bounces = false. But for some reason it has no affect in my webView at all.

Help anyone?

i.e. I prefer solving the issue with native swift rather then injecting js etc..

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Hudi Ilfeld
  • 1,905
  • 2
  • 16
  • 25
  • Have you tried setting to offset as in this answer: https://stackoverflow.com/questions/36407633/how-to-stop-uiwebview-from-bouncing-vertically-scrolling-bottom – DocForNoc Mar 25 '18 at 14:20
  • @DocForNoc I try'd it but didn't work. I also noticed something wierd for some reason the `scrollViewDidScroll` doesn't get called at all. despite the fact that I set `wkWebView.scrollView.delegate = self` – Hudi Ilfeld Mar 25 '18 at 15:04
  • I guess the problem is somewhere with the web code which is disabling certain scrollview functions. it's clearly not a problem with my swift code. – Hudi Ilfeld Mar 25 '18 at 20:13
  • Could be. You can load a simple HTML into your WKWebView instance and test if there is any difference and the behaviour and receiving the delegate methods. – DocForNoc Mar 27 '18 at 10:53

3 Answers3

23

This is work for me:

wKWebView.scrollView.bounces = false
missionMan
  • 873
  • 8
  • 21
  • 1
    Are you serious? OP said *What most people suggest is to use the following: wkWebView.scrollView.bounces = false. But for some reason it has no affect in my webView at all*. – LinusGeffarth Sep 04 '18 at 13:16
  • 1
    :) I agree that I wrote the problem before I read it. But Yes, i'm still serious. I needed the same thing. But there was no answer on the first screen that came up with the search result. After I solved my problem, I wrote the code that worked for me. Do not leave an old question unanswered, it will help many people to help those who like me. What's wrong with that? This answer is a response to %99.9 when searched according to the title of the problem. – missionMan Sep 05 '18 at 11:45
  • If bounces = NO isn't working, it's possible your webview wasn't initialized. Verify that your webview isn't nil, when you disable bounce. – Jonathan Chandler Sep 02 '19 at 15:56
  • What would be the equivalent command in obj-c? I'm using [cordova](https://cordova.apache.org/docs/en/latest/) and it's still relying on obj-c for ios. – Uche Ozoemena Jan 20 '22 at 00:14
5

for me, it works when I set these two:

webView.scrollView.alwaysBounceVertical = false
webView.scrollView.bounces = false

but some H5 still bouncing, I guess it's the situation caused by the website itself.

sunshuyao
  • 82
  • 1
  • 4
3

This may be a problem with where you are doing this. This does not work when executed in the LoadView function. This only works if it is run in the ViewDidLoad function.

    override func viewDidLoad() {
        super.viewDidLoad()

        mainWebView.scrollView.bounces = false
        ...
hong developer
  • 13,291
  • 4
  • 38
  • 68