菜鸟教程小白 发表于 2022-12-11 18:18:03

ios - 为什么我的模拟以编程方式获得与 anchor /约束有关的空白黑屏?


                                            <p><p>这是我的代码。我试图将 webViewanchor 的顶部限制在 ViewController 顶部布局指南的底部,但我只是得到一个黑屏,有时控制台中出现 <strong>错误:</strong> </p>

<pre><code> Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don&#39;t want.
    Try this:
      (1) look at each constraint and try to figure out which you don&#39;t expect;
      (2) find the code that added the unwanted constraint or constraints and fix it.
    (Note: If you&#39;re seeing NSAutoresizingMaskLayoutConstraints that you don&#39;t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
    &#34;&lt;_UILayoutSupportConstraint:0x608000284d30 _UILayoutSpacer:0x6080001aa560&#39;UIVC-topLayoutGuide&#39;.height == 20   (active)&gt;&#34;,
    &#34;&lt;_UILayoutSupportConstraint:0x6080002850a0 V:|-(0)-   (active, names: &#39;|&#39;:UIView:0x7fcfcac26cd0 )&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0x608000280fa0 h=--&amp; v=--&amp; WKWebView:0x7fcfcb09a400.midY == 0   (active)&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0x608000280ff0 h=--&amp; v=--&amp; WKWebView:0x7fcfcb09a400.height == 0   (active)&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x6080002846f0 V:-(3)-   (active)&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0x608000281220 h=--&amp; v=--&amp; &#39;UIView-Encapsulated-Layout-Top&#39; UIView:0x7fcfcac26cd0.minY == 0   (active, names: &#39;|&#39;:UIViewControllerWrapperView:0x7fcfcac08120 )&gt;&#34;
)
</code></pre>

<p><strong>代码 -</strong></p>

<pre><code>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: &#34;https://www.google.com&#34;)
      if let selectedURL = url {
            let request = NSURLRequest(url: selectedURL as URL) as URLRequest
            webView.load(request)
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>添加导航委托(delegate):</p>

<pre><code>webView.navigationDelegate = self
</code></pre>

<p>然后听错误和导航代表,你将能够看到真正发生或没有发生的事情。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么我的模拟以编程方式获得与 anchor /约束有关的空白黑屏?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41045099/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41045099/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么我的模拟以编程方式获得与 anchor /约束有关的空白黑屏?