I am showing a pdf file in WKWebView in IOS swift, and it showing fine. I am loading pdf file from server. But some part of file is being hidden behind top navigation bar. I want to add margin at top of WKWebView. Here is my current code.
let myBlog = file
let url = NSURL(string: myBlog)
let request = NSURLRequest(url: url! as URL)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
// webView.translatesAutoresizingMaskIntoConstraints = false
// webView.addConstraints([NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)])
self.view.addSubview(sv)
let pdfVC = UIViewController()
pdfVC.view.addSubview(webView)
pdfVC.title = "File"
self.navigationController?.pushViewController(pdfVC, animated: true)
Here the commented code is how I am trying to add margin and not working.
webView = WKWebView(frame: self.view.frame)
在上面的行中,设置框架,使其离开顶部边缘,并从高度减小给定边缘。
尝试设置layoutMargin,它应该可以解决问题。
self.webView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
First of all set translatesAutoresizingMaskIntoConstraints
of webView
to false
, ie
webView.translatesAutoresizingMaskIntoConstraints = false
Now, add proper constraints
of webView
to view's
safeAreaLayoutGuide
or layoutMarginsGuide
, ie
var guide: UILayoutGuide
if #available(iOS 11.0, *) {
guide = self.view.safeAreaLayoutGuide
} else {
guide = self.view.layoutMarginsGuide
}
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
webView.topAnchor.constraint(equalTo: guide.bottomAnchor),
webView.bottomAnchor.constraint(equalTo: guide.bottomAnchor)
])
您可以使用以下行添加上边距
webView = WKWebView(frame: CGRect(x: 0, y: 40, width: self.view.bounds.width, height: self.view.bounds.height - 40)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.