user2423476
user2423476

Reputation: 2275

Get scroll position of webview in swift

I'm trying to get the scroll position from a webview in swift. I want to obtain the position when scrolling. Everytime I start scrolling I would like to get the position of the current scroll and the height of the total scroll. How can this be accomplished?

Upvotes: 1

Views: 5264

Answers (3)

Nirbhay Singh
Nirbhay Singh

Reputation: 1298

try this,

ScrollView contentOffset defines the point of origin of the content view

let x_OffsetValue = webView.scrollView.contentOffset.x

let y_OffsetValue = webView.scrollView.contentOffset.y

Upvotes: 1

Harshal Valanda
Harshal Valanda

Reputation: 5451

The scroll view associated with the WKWebView and UIWebView

you can get webView total scroll contentSize as follow

webView.scrollView.contentSize

current scroll position

webView.scrollView.contentOffset

Upvotes: 1

Vandana pansuria
Vandana pansuria

Reputation: 764

Try this, you can get scroll position of UIWebview:

func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.size.height < 120 {
            print("the end")
        } else if scrollView.contentOffset.y < 120 {
            print("the beginning")
        }
    }

Upvotes: 0

Related Questions