2

I used following code to paginate the UIWebView.

self.webView.paginationMode = UIWebPaginationModeLeftToRight;
self.webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
self.webView.scrollView.pagingEnabled = YES;

Now I want to know the total number of pages it created. The IOS7 document says UIWebView has a property called pageCount. I tried it, but it always returns 1.

How can I get the number of pages it created after pagination?

Thanks.

2 Answers 2

4

If you want only the number of pages you can use CoreGraphics

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:@"your path"]);
int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFDocumentRelease(pdf);

If you want to count pages for HTML content you need to set delegate

self.webView.delegate = self;

and to implement UIWebViewDelegate method

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSUInteger count1 = webView.pageCount; // for iOS7 only!

    NSUInteger count2 = webView.scrollView.contentSize.width / webView.scrollView.frame.size.width; // for horizontal paging

    NSUInteger count3 = webView.scrollView.contentSize.height / webView.scrollView.frame.size.height; // for vertical paging
}

Note that you can get pageCount only after full rendering of the requested HTML page.

7
  • I am using UIWebView to display HTML instead of PDF. Is there a way to show the total paginated page numbers?
    – user890207
    Jan 26, 2014 at 15:21
  • Thanks. This works. I also notice I can put pageCount inside viewDidAppear and it also works.
    – user890207
    Jan 26, 2014 at 16:22
  • Exactly. Rendering is done by the time of viewDidAppear. But if you want to load pages by some user request you need delegates.
    – malex
    Jan 26, 2014 at 18:08
  • Note that you can set the pageLength property to split in multiple columns Oct 2, 2014 at 15:42
  • @malex: Will this work for document as well like word, excel which i am displaying in UIWebView. I am looking for Pagecount and horizontal/Vertical scrolling of these documents.
    – Arun Gupta
    Mar 7, 2015 at 11:00
1

UIWebView is a subclass of UIScrollView. You can detect UIWebView page change event for scroll view delegate method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

This delegate will be called each time the webView scrolls. Maintain your page number yourself. You can use scrollView.contentOffset.x and scrollView.contentOffset.y to determine the scrolling direction.

You can find more help here.

EDIT We can find page number of scrollview from contentoffset. Try something similar for webView. int page = scrollView.contentOffset.x / scrollView.frame.size.width;

2
  • I need to know the total number of pages before I scroll the page. I need to pass this value to the PageControl display the dots. In fact, I used similar function to get page self.pageControl.numberOfPages = self.webView.scrollView.contentSize.width / pageWidth;, but the scrollView.contentSize.width only shows one page width. I would expect it shows the width of all pages.
    – user890207
    Jan 26, 2014 at 15:18
  • Did you tried scrollView.contentOffset.x inted of contentSize.width??
    – Rashad
    Jan 26, 2014 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.