title: Masking a UIView and Auto Layout
body: <p>I have a UIView that I want to mask with another UIView, punching a hole out of its center. Here's my <code>viewDidLoad</code>:</p>
  
  <pre><code>- (void)viewDidLoad {
  [super viewDidLoad];
  [self.view addSubview:self.viewToMask];
  [self.view addSubview:self.theMask];
  
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToMask attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToMask attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[cyan(200)]" options:0 metrics:nil views:@{@"cyan": self.viewToMask}]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[cyan(200)]" options:0 metrics:nil views:@{@"cyan": self.viewToMask}]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.theMask attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.viewToMask attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.theMask attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.viewToMask attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[mask(100)]" options:0 metrics:nil views:@{@"mask": self.theMask}]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[mask(100)]" options:0 metrics:nil views:@{@"mask": self.theMask}]];
  }
  </code></pre>
  
  <p>It gives me exactly what I'm looking for, minus the masking:</p>
  
  <p><img src="https://i.stack.imgur.com/8NZse.png" alt="views without masking"></p>
  
  <p>If I add one more line:</p>
  
  <pre><code>[self.viewToMask setMaskView:self.theMask];
  </code></pre>
  
  <p>both views disappear --- the small view (<code>self.theMask</code>) masks out the whole larger view (<code>self.viewToMask</code>) even though it's only half the size. Does anyone understand what's going on here? Can you not use <code>UIView.maskView</code> with Auto Layout?</p>