I have horizontal scrolling in a webView
. I need that my text in the webView
to be shown from the position x = 1000
. For this I use the following line:
self.webView.scrollView.contentOffset.x = 1000
The problem is that this line only works in the scrollViewDidScroll
method, and if I use it in method, then after that I cannot scroll my webView
because it is always on x = 1000
In viewDidLoad
the line does not work. Where I need to write it so that it works at the start of webView
, and then I can scroll my webView correctly.
code:
import UIKit
import WebKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView?.scrollView.delegate = self
do {
guard let filePath = Bundle.main.path(forResource: "index", ofType: "html")
else {
print ("File reading error")
return
}
let headerString = "<meta name=\"viewport\" content=\"initial-scale=1.0\" />"
let string = try String(contentsOfFile: filePath, encoding: .utf8)
let baseUrl = URL(fileURLWithPath: filePath)
webView.loadHTMLString(headerString+string, baseURL: baseUrl)
webView.scrollView.isPagingEnabled = true
self.webView.scrollView.contentOffset.x = 1000
}
catch {
print ("File HTML error")
}
}
}
This line also does not work in viewDidLoad
webView.scrollView.setContentOffset(CGPoint(x: 1000, y: 0), animated: false)