ios – WKWebView setContentOffset doesn’t work with animated = false: no scrolling happens


I’m trying to create an EPUB reader. For the reason that chapters in an EPUB are represented as entire HTML information with no web page breaks, I’ll show one web page at a time in a WKWebView, loading the string with loadHTMLString. Then, when the person swipes to the subsequent web page of the UIPageViewController, I’ll make a brand new WKWebView and scroll down a web page utilizing the setContentOffset technique. Nonetheless, the setContentOffset technique has no impact on the displayed content material. tldr – how can I inform when this setContentOffset technique is secure to name?

Right here is my web page view controller:

class MyPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    var web page = 0
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        dataSource = self
        delegate = self
        
        setViewControllers([Page(page: 0)], route: .ahead, animated: false, completion: nil)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        return Web page(web page: web page + 1)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating completed: Bool, previousViewControllers: [UIViewController], transitionCompleted accomplished: Bool) {
        if accomplished {
            web page += 1
        }
   }
}

and right here is my Web page class:

class Web page: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var web page: Int
    var webView: WKWebView!
    
    let testString = "<h1>00000000000000000000000000000000000000000000000000</h1><br><h1>11111111111111111111111111111111111111111111111111</h1><br><h1>22222222222222222222222222222222222222222222222222</h1><br><h1>33333333333333333333333333333333333333333333333333</h1><br><h1>44444444444444444444444444444444444444444444444444</h1><br><h1>55555555555555555555555555555555555555555555555555</h1><br><h1>66666666666666666666666666666666666666666666666666</h1><br><h1>77777777777777777777777777777777777777777777777777</h1><br><h1>88888888888888888888888888888888888888888888888888</h1><br><h1>99999999999999999999999999999999999999999999999999</h1>"

    init(web page: Int) {
        self.web page = web page
        tremendous.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        webView = WKWebView(body: view.bounds)
        webView.navigationDelegate = self
        
        view.addSubview(webView)
        
        load()
    }
    
    func load() {
        webView.loadHTMLString(testString, baseURL: nil)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.scroll()
    }
    
    func scroll() {
        let scrollPoint = CGPoint(x: 0, y: 100 * CGFloat(web page))
        webView.scrollView.setContentOffset(scrollPoint, animated: false)
    }
}

I would like the scroll to be instantaneous in order that the person can not inform it’s scrolling, therefore the animated: false. Nonetheless, this merely doesn’t change the view in any method. If I take advantage of animated: true it really works positive, and if I wrap self.scroll() in a DispatchQueue.most important.asyncAfter(deadline: .now() + 0.1) it really works as effectively. However the person can see a little bit of a jarring scroll if I do that. I’ve tried calling .setNeedsLayout() and .layoutIfNeeded() to no avail; the view nonetheless doesn’t change.

So clearly there’s something that isn’t able to scroll the webView, even after the didFinish delegate technique is named. How can I inform when this webView is definitely able to scroll? Or is there another excuse that animated: false will not be working?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles