Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView, scrolling the web page to top

I tried this, which should work but doesn't:

webView.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)

I am working on a browser and have a couple of webViews in kept in stack. Some sites work as expected, start at the top, some do not.

Starts at top: http://leagueoflegends.com/

enter image description here

This one starts a little down, have to manually scroll up: http://euw.leagueoflegends.com/

enter image description here

Extra note, this problem never happens to the first webView. Even if I load the 2nd link and show it as first webView, it doesn't scroll to the middle.

like image 484
Esqarrouth Avatar asked Oct 09 '15 15:10

Esqarrouth


People also ask

What is a WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I turn off bouncing effect in WKWebView?

What most people suggest is to use the following: wkWebView. scrollView. bounces = false .

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.

How do I migrate to WKWebView?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


2 Answers

I encountered this too. Finally, I fixed this way:

if #available(iOS 11, *) {
    webView.scrollView.contentInsetAdjustmentBehavior = .never
}

If you have this issue on iOS 11, try this.

like image 87
Benjamin Wen Avatar answered Sep 24 '22 13:09

Benjamin Wen


Before iOS 11.0, UIWebView was avilable with delegates to indicate that loading of webView is finished and we can do any operation after loading has been finished.

Now we have WKWebView which doesn't provide any delegate method to notify once your webView has finished loading. Due to this if we do following piece of code then it will not work:

let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
webView.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)

In this case, webView.load() is a asynchronous function which gets called and code moves further without waiting for its completion. Due to this your call for setContentOffset work but after that when webView finish loading it reset the ContentOffset to default, which is actually (0,0) but I am not sure why your WebView doesn't load from top. But doing code in following manner will may help you:

Extend to WKNavigationDelegate and implement didFinish navigation method in your ViewController and call setContentOffset in that method.

class ViewController: UIViewController, WKNavigationDelegate {

      override func viewDidLoad() {
          let myURL = URL(string: "https://www.google.com")
          let myRequest = URLRequest(url: myURL!)
          webView.load(myRequest)
          webView.navigationDelegate = self
      }

      func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
      }
} 
like image 31
Akshay Jadhav Avatar answered Sep 25 '22 13:09

Akshay Jadhav