这是我的代码。我试图将 webView anchor 的顶部限制在 View Controller 顶部布局指南的底部,但我只是得到一个黑屏,有时控制台中出现 错误:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<_UILayoutSupportConstraint:0x608000284d30 _UILayoutSpacer:0x6080001aa560'UIVC-topLayoutGuide'.height == 20 (active)>",
"<_UILayoutSupportConstraint:0x6080002850a0 V:|-(0)-[_UILayoutSpacer:0x6080001aa560'UIVC-topLayoutGuide'] (active, names: '|':UIView:0x7fcfcac26cd0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x608000280fa0 h=--& v=--& WKWebView:0x7fcfcb09a400.midY == 0 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x608000280ff0 h=--& v=--& WKWebView:0x7fcfcb09a400.height == 0 (active)>",
"<NSLayoutConstraint:0x6080002846f0 V:[_UILayoutSpacer:0x6080001aa560'UIVC-topLayoutGuide']-(3)-[WKWebView:0x7fcfcb09a400] (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x608000281220 h=--& v=--& 'UIView-Encapsulated-Layout-Top' UIView:0x7fcfcac26cd0.minY == 0 (active, names: '|':UIViewControllerWrapperView:0x7fcfcac08120 )>"
)
代码 -
import UIKit
import WebKit
class webViewController: UIViewController, WKNavigationDelegate {
override func loadView() {
self.view = UIView()
let webView = WKWebView()
// webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let topConstraint = webView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 3)
topConstraint.isActive = true
let url = NSURL(string: "https://www.google.com")
if let selectedURL = url {
let request = NSURLRequest(url: selectedURL as URL) as URLRequest
webView.load(request)
}
}
}
Best Answer-推荐答案 strong>
添加导航委托(delegate):
webView.navigationDelegate = self
然后听错误和导航代表,你将能够看到真正发生或没有发生的事情。
关于ios - 为什么我的模拟以编程方式获得与 anchor /约束有关的空白黑屏?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/41045099/
|