title: Cut a circle out of a UIView using mask
body: <p>In my app I have a square UIView and I want to cut a hole/notch out of the top of. All the tutorials online are all the same and seemed quite straightforward but every single one of them always delivered the exact opposite of what I wanted.</p>
  
  <p>For example this is the code for the custom UIView:</p>
  
  <pre><code>class BottomOverlayView: UIView {
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          drawCircle()
      }
  
      fileprivate func drawCircle(){
  
          let circleRadius: CGFloat = 80
          let topMidRectangle = CGRect(x: 0, y: 0, width: circleRadius*2, height: circleRadius*2)
  
          let circle: CAShapeLayer = CAShapeLayer()
          circle.position = CGPoint(x: (frame.width/2)-circleRadius, y: 0-circleRadius)
          circle.fillColor = UIColor.black.cgColor
          circle.path = UIBezierPath(ovalIn: topMidRectangle).cgPath
          circle.fillRule = kCAFillRuleEvenOdd
  
          self.layer.mask = circle
          self.clipsToBounds = true
      }
  }
  </code></pre>
  
  <p>Here is what I hope to achieve (the light blue is the UIView, the dark blue is the background):</p>
  
  <p><a href="https://i.stack.imgur.com/46NF9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/46NF9.png" alt="What I want"></a></p>
  
  <p>But here is what I get instead. (Every single time no matter what I try)</p>
  
  <p><a href="https://i.stack.imgur.com/3ZNc5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3ZNc5.png" alt="What I get"></a></p>
  
  <p>I'm not sure how I would achieve this, aside from making a mask that is already the exact shape that I need. But if I was able to do that then I wouldn't be having this issue in the first place. Does anyone have any tips on how to achieve this?</p>
  
  <p><strong>EDIT:</strong> The question that this is supposedly a duplicate of I had already attempted and was not able to get working. Perhaps I was doing it wrong or using it in the wrong context. I wasn't familiar with any of the given methods and also the use of pointers made it seem a bit outdated. The accepted answer does a much better job of explaining how this can be implemented using much more widely used UIBezierPaths and also within the context of a custom UIView.</p>