Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WKWebView Horizontal Paging #1

Open
ahmedk92 opened this issue May 29, 2020 · 0 comments
Open

WKWebView Horizontal Paging #1

ahmedk92 opened this issue May 29, 2020 · 0 comments

Comments

@ahmedk92
Copy link
Owner

(Originally published 2017-11-03)

UIWebView had a helpful property named paginationMode. It gave you horizontal pagination out of the box; specifically using the values: leftToRight, and rightToLeft. However, UIWebView is going out of favor, and since iOS 8, Apple officially encourages using WKWebView. Unfortunately, WKWebView doesn't have that property nor its equivalent out of the box.

Thankfully, horizontal pagination (left and right) is doable using CSS. CSS has a helpful property called column-width. Using that results in segmenting the html body into columns of the specified width. So, the idea is as follows:

  1. Set the column-width property of the body to the webview's width.
  2. Set the height property of the body to the webview's height.
  3. Set isPagingEnabled of the webview's scrollView to true.

That's it (for left to right content though). It's up to you where to set the above values. For example, you can do it in webView(_:didFinish:) of the webview's navigationDelegate.

Now, RTL support.

It's as simple as setting the direction style property of the html tag to rtl. But if that somehow badly affects your page, then you have to get more creative. One crazy solution is as follows:

  1. Wrap all the contents of the <body> tag in one container div.
  2. Set the -webkit-transform property of that div to scale(-1, 1). (i.e. this results in horizontal mirroring)
  3. Similarily, mirror the webview itself. (i.e. webview.transform = CGAffeineTransform(scaledX: -1, y: 1))

Credits:

Thanks to my friend and colleague Sayed Arfa for suggesting column-width and overall inspiration.

Update

At the time of writing this post, I somehow missed this thread. They suggested to use the undocumented(?) CSS property value overflow:-webkit-paged-x. This is more convenient than what's explained above as it doesn't need us to compute a width nor do additional work for RTL handling. However, it doesn't seem reliable, as the chromium project seems to plan to remove it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant