【发布时间】:2021-03-23 14:12:32
【问题描述】:
我马上开始了。
代码是一个没有高级导航的webview,约束从顶部移动webview,留下一个上边距。
看图
我的代码
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate, WKUIDelegate {
lazy var webView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
func setupUI() {
self.view.backgroundColor = .red
self.view.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
webView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
webView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor)
])
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
let myURL = URL(string: "https://www.google.com.br/")
let myRequest = URLRequest(url: myURL!,cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy,timeoutInterval: 10.0)
webView.load(myRequest)
}
}
【问题讨论】:
-
尝试从webView顶部移除安全区域约束,将其更改为self.view.topAnchor
-
我试过修改。 NSLayoutConstraint.activate ([ webView.topAnchor.constraint (equalTo: self.view.topAnchor), webView.leftAnchor.constraint (equalTo: self.view.leftAnchor), webView.bottomAnchor.constraint (equalTo: self.view.bottomAnchor), webView.rightAnchor.constraint (equalTo: self.view.rightAnchor) ]) 边距可见。
-
您可以添加自己的答案,然后将问题标记为
solved,这样其他人就会看到这个问题的答案并知道有解决方案。 stackoverflow.com/help/self-answer