programmatic constraints - cover entire superview using layout anchors
<p>Is it possible using layout anchors to cover an entire superview's screen?</p>

<p>Using NSLayoutConstraints, I can achieve this effect using the code below</p>

<pre><code>dimmingView = UIView()
dimmingView.translatesAutoresizingMaskIntoConstraints = false
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)

containerView?.addSubview(dimmingView)

NSLayoutConstraint(item: containerView!, attribute: .leadingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .trailingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .topMargin, relatedBy: .equal, toItem: dimmingView, attribute: .topMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .bottomMargin, relatedBy: .equal, toItem: dimmingView, attribute: .bottomMargin, multiplier: 1, constant: 0).isActive = true
</code></pre>

<p>I want to switch to layout anchors instead, following what's suggested in the <a href="https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html" rel="nofollow">docs</a>. I tried doing this:</p>

<pre><code>let margins = containerView?.layoutMarginsGuide

dimmingView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
dimmingView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
dimmingView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
dimmingView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
</code></pre>

<p>or this</p>

<pre><code>let margins = containerView?.layoutMarginsGuide

dimmingView.heightAnchor.constraint(equalTo: margins.heightAnchor).isActive = true
dimmingView.widthAnchor.constraint(equalTo: margins.widthAnchor).isActive = true
dimmingView.centerXAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true
dimmingView.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
</code></pre>

<p>but both did not work. Instead of fully covering containerview, what I end up with are paddings at all 4 sides.</p>

<p><a href="https://i.stack.imgur.com/0a6k0.png" rel="nofollow"><img src="https://i.stack.imgur.com/0a6k0.png" alt="enter image description here"></a></p>