title: Adding border to mask layer
body: <p>I'm trying to make a custom shape <code>UIButton</code> using mask layers and I was successful </p>
  
  <pre><code>extension UIButton { 
  
    func mask(withImage image : UIImage , frame : CGRect ){
  
          let maskingLayer = CAShapeLayer()
          maskingLayer.frame = frame
          maskingLayer.contents = image.cgImage
          self.layer.mask = maskingLayer
      }
  }
  </code></pre>
  
  <p>But I want to add a border (stroke) to the mask layer to indicate that this button was chosen.</p>
  
  <p>I tried adding a sub layer to the mask layer </p>
  
  <pre><code>button.mask?.layer.borderColor = UIColor.black.cgColor
  button.mask?.layer.borderWidth = 4
  </code></pre>
  
  <p>but didn't work since the button shape is still rectangle.</p>
  
  <p>I know I can use <code>CGMutablePath</code> to define the shape</p>
  
  <pre><code>func mask(withPath path: CGMutablePath , frame : CGRect , color : UIColor) {
  
      let mask = CAShapeLayer()
      mask.frame = frame
      mask.path = path
      self.layer.mask = mask
  
      let shape = CAShapeLayer()
      shape.frame = self.bounds
      shape.path = path
      shape.lineWidth = 3.0
      shape.strokeColor = UIColor.black.cgColor
      shape.fillColor = color.cgColor
  
      self.layer.insertSublayer(shape, at: 0)
     //  self.layer.sublayers![0].masksToBounds = true
  }
  </code></pre>
  
  <p>but drawing such complex shape using paths is extremly hard</p>
  
  <p><a href="https://i.stack.imgur.com/vSW8Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vSW8Q.png" alt="enter image description here"></a></p>
  
  <p>Any help would be appreciated.</p>