HomeiOS Developmentios - WKWebView setContentOffset doesn't work with animated = false: no scrolling...

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 complete HTML recordsdata 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 consumer 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 methodology. Nonetheless, the setContentOffset methodology has no impact on the displayed content material. tldr – how can I inform when this setContentOffset methodology is protected 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)], course: .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 consumer can not inform it’s scrolling, therefore the animated: false. Nonetheless, this merely doesn’t change the view in any approach. If I take advantage of animated: true it really works positive, and if I wrap self.scroll() in a DispatchQueue.primary.asyncAfter(deadline: .now() + 0.1) it really works as nicely. However the consumer 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 methodology is known as. How can I inform when this webView is definitely able to scroll? Or is there one more reason that animated: false isn’t working?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments