6

I can not get to a solution after much googling. I have a UIWebView with a PDF in it.

I do not want the view: "Page 1 of 20" The small view in the upper left corner when viewing a PDF file. It is possible to remove this?

olivier
  • 2,585
  • 6
  • 34
  • 61
  • page number hint doesn't look like JS thing. You can take a look on similar issue. http://stackoverflow.com/a/4167060/729175 You can try to walk through hidden .subviews and find what you want. – Chugaister Jan 20 '14 at 10:32
  • while this could be possible to achieve via traversing the subviews, it isn't a very elegant solution. In my opinion it would be beetter to use a dedicated iOS PDF Reader Component like PSPDFKit(commercial), vfrreader or FastPDFKit to name a few – JonEasy Aug 24 '15 at 14:12

4 Answers4

2

Swift 3 and WKWebView

I get all the subviews of the WKWebView and then remove WKPDFPageNumberIndicator:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  let webViewSubviews = self.getSubviewsOfView(v: self.webView)
  for v in webViewSubviews {
    if v.description.range(of:"WKPDFPageNumberIndicator") != nil {
      v.isHidden = true // hide page indicator in upper left
    }
  }
}

func getSubviewsOfView(v:UIView) -> [UIView] {
  var viewArray = [UIView]()
  for subview in v.subviews {
    viewArray += getSubviewsOfView(v: subview)
    viewArray.append(subview)
  }
  return viewArray
}

You'll probably have to set webView.navigationDelegate = self

UPDATE (not tested) - Swift 4

Keep the getSubviewsOfView function from above and modify the webView function like this:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  let webViewSubviews = self.getSubviewsOfView(v: self.webView)
  for v in webViewSubviews {
    if v.isKind(of: UILabel.self) || v.isKind(of: UIImageView.self) || v.isKind(of: UIVisualEffectView.self) {
      v.isHidden = true
    }
  }
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
  • @MassimoPolimeni it's been a long time so it's possible that the WKPDFPageNumberIndicator has been renamed in newer versions of swift. I don't have a project that I could quickly run to test this out and help you :/ Try going through all the subviews and hiding all the UIImageView, UILabel and UIVisualEffectView as some other answers suggest. – budiDino Mar 22 '19 at 16:28
  • @MassimoPolimeni check out the modification I did above and please let me know if that works for you. – budiDino Mar 22 '19 at 16:34
  • I changed a little bit to match Swift 4.2 syntax so placing `v.isKind(of: UILabel.self)` but it still doesn't work, not sure why I'm debugging it, the main idea seems legitimate – Massimo Polimeni Mar 22 '19 at 16:43
  • I remember that I basically printed out `v.description` for all the subviews and then I detected something containing `WKPDFPageNumberIndicator`. Good luck ;) – budiDino Mar 22 '19 at 17:03
  • Not work :/. Not able to find `WKPDFPageNumberIndicator` class. iOS 13.3 Using `WKWebview`. – Tiago Amaral Jan 28 '20 at 19:52
1

This code is working on iOS9, 10 (tested)

    func hidePageNumberView(v: UIView) {
        for subView in v.subviews {
            if subView.isKindOfClass(UIImageView) || subView.isKindOfClass(UILabel) || subView.isKindOfClass(UIVisualEffectView){
                subView.hidden = true

                if subView.isKindOfClass(UILabel) {
                    if let sv = subView.superview {
                        sv.hidden = true
                    }
                }
            } else {
                hidePageNumberView(subView)
            }
        }
    }

Implement webView.scrollView.delegate and run above code in scrollViewDidScroll

Hope this help.

iOS 11 / Swift 4:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    hidePageNumberView(scrollView)
}

func hidePageNumberView(_ v: UIView) {
    for subView in v.subviews {
        if subView is UIImageView || subView is UILabel || subView is UIVisualEffectView {
            subView.isHidden = true

            if subView is UILabel {
                if let sv = subView.superview {
                    sv.isHidden = true
                }
            }
        } else {
            hidePageNumberView(subView)
        }
    }
}
Politta
  • 400
  • 4
  • 10
lee5783
  • 434
  • 4
  • 12
1

After a while I've found a solution using swift 5 and WKWebView together with my partner:

let pdfWebView: WKWebView = {
     let webView = WKWebView()
     webView.backgroundColor = .clear
     webView.isOpaque = false
     webView.contentMode = .scaleToFill
     return webView
}()

pdfWebView.delegate = self
pdfWebView.navigationDelegate = self

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     guard let last = webView.subviews.last else {
          return
     }

     last.isHidden = true
}
sidneivl
  • 242
  • 3
  • 12
-1

I found a solution to your question... You can Hide the "Page 1 of 20" using following code :

for (UIView* subView in [webView subviews])
{
    if ([subView isKindOfClass:[UIScrollView class]]) {
        for (UIView* shadowView in [subView subviews])
        {
            if ([shadowView isKindOfClass:[UIImageView class]]) {
                [shadowView setHidden:YES];
            }
        }
    }
}

Hope this may be helpful to some one... Thanks...

Kanan Vora
  • 224
  • 1
  • 9