title: UIView with shadow, rounded corners and custom drawRect
body: <p>I have to create a custom <code>UIView</code> that will have round corners, a border, a shadow and its <code>drawRect()</code> method is overridden to provide custom drawing code with which several straight lines are drawn into the view (I need to use a fast, lightweight approach here since many of these views may be rendered).</p>
  
  <p>The problem I'm currently facing is that the shadow doesn't apply anymore to the round corners as soon as I override <code>drawRect()</code> in the view class (even without any custom code yet in it). See the attached image for the difference:</p>
  
  <p><img src="https://i.stack.imgur.com/R5HOn.jpg" alt="enter image description here"></p>
  
  <p>In the view controller I'm using the following code:</p>
  
  <pre><code>    view.layer.cornerRadius = 10;
      view.layer.masksToBounds = true;
  
      view.layer.borderColor = UIColor.grayColor().CGColor;
      view.layer.borderWidth = 0.5;
  
      view.layer.contentsScale = UIScreen.mainScreen().scale;
      view.layer.shadowColor = UIColor.blackColor().CGColor;
      view.layer.shadowOffset = CGSizeZero;
      view.layer.shadowRadius = 5.0;
      view.layer.shadowOpacity = 0.5;
      view.layer.masksToBounds = false;
      view.clipsToBounds = false;
  </code></pre>
  
  <p>In the overridden <code>drawContext()</code> I would use something like:</p>
  
  <pre><code>    var context:CGContext = UIGraphicsGetCurrentContext();
      CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor);
      // Draw them with a 2.0 stroke width so they are a bit more visible.
      CGContextSetLineWidth(context, 2.0);
      CGContextMoveToPoint(context, 0.0, 0.0); //start at this point
      CGContextAddLineToPoint(context, 20.0, 20.0); //draw to this point
      CGContextStrokePath(context);
  </code></pre>
  
  <p>But as said above, the shadow problem occurs even without this code added.</p>
  
  <p>Is there any other/better way to draw lightweight elements onto a view other than this approach that is compatible with round corners and shadows? I don't want to add any unnecessary extra views or image contexts to the view since these need to be light and performant.</p>