使用Swift5,
我尝试将WKWebview的背景设置为透明。
不幸的是,现在,在Web视图中显示.pdf文件时,颜色始终为灰色!但是我想将其更改为透明。我该怎么办?
我使用以下代码:
class SomeViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
var document: Document!
var documentName: String!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
// I try this - but it does not work...
// WHY WHY WHY WHY ???????????????????????
webView.isOpaque = false
webView.backgroundColor = UIColor.clear
webView.scrollView.backgroundColor = UIColor.clear
BusyView.show()
// here the .pdf file gets loaded
// the file is loaded correctly but its background is still gray (instead of transparent)...
if let fullURL = NSURL.fileURL(withPathComponents: [NSTemporaryDirectory(), documentName]) {
let request = URLRequest(url: fullURL)
webView.load(request)
}
navigationItem.title = document?.title
navigationItem.largeTitleDisplayMode = .never
}
// ....
}
extension MailboxViewDocumentController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
BusyView.hide()
scrollToDistanceFromUnderneathBlurredTopBar()
}
fileprivate func scrollToDistanceFromUnderneathBlurredTopBar() {
var scrollDistance: CGFloat
if #available(iOS 11, *) {
scrollDistance = view.safeAreaInsets.top
} else {
scrollDistance = topLayoutGuide.length
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) {
let scrollPoint = CGPoint(x: 0, y: -scrollDistance)
self.webView.scrollView.setContentOffset(scrollPoint, animated: false)
}
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
BusyView.hide()
}
}