在显示pdf时,是否可以在UIwebview中删除[“Page 1 of 20”视图]?

时间:2014-01-19 16:44:57

标签: ios pdf uiwebview

经过谷歌搜索后,我无法找到解决方案。 我有一个带有PDF的UIWebView。

我不想要视图:“第1页,共20页”查看PDF文件时左上角的小视图。 可以删除这个吗?

4 个答案:

答案 0 :(得分:2)

Swift 3和WKWebView

我获得WKWebView的所有子视图,然后移除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
}

您可能需要设置webView.navigationDelegate = self

更新(未测试) - Swift 4

保持上面的getSubviewsOfView功能并修改webView功能,如下所示:

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
    }
  }
}

答案 1 :(得分:1)

此代码适用于iOS9,10(已测试)

    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)
            }
        }
    }

实施webView.scrollView.delegate并在scrollViewDidScroll

中运行上述代码

希望得到这个帮助。

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)
        }
    }
}

答案 2 :(得分:0)

一段时间后,我发现与我的伙伴一起使用swift 5和WKWebView的解决方案:

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
}

答案 3 :(得分:-1)

我找到了你问题的解决方案...... 您可以使用以下代码隐藏“第1页,共20页”:

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];
            }
        }
    }
}

希望这对某些人有帮助...... 感谢...