title: iOS stroke layer outside of it's bounds
body: <p>I'm trying to build a category method on <code>UIView</code> to allow to stroke the <code>UIView</code>, similarly to the Photoshop function: <strong>Stroke-Outside</strong>. While <code>CALayer</code> properties borderWidth and borderColor work flawlessly to stroke on the inside, I cannot find a good way to stroke the view on the outside.</p>
  
  <p><strong>This is the solution I came up at first:</strong></p>
  
  <p><em>Using sublayers I add another <code>CALayer</code> with frame larger than the superlayer's bounds. The layer is then inserted in the <code>UIView</code>'s sublayers.</em></p>
  
  <pre><code>- (void)strokeViewWithColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
  {
      CALayer *borderLayer = [CALayer layer];
      CGRect borderFrame = CGRectMake(-borderWidth, -borderWidth, (self.frame.size.width) + (borderWidth * 2), (self.frame.size.height) + (borderWidth * 2));
      [borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]];
      [borderLayer setFrame:borderFrame];
  
      //
      // Copy current border layer radius
      //
      [borderLayer setCornerRadius:self.layer.cornerRadius];
  
      [borderLayer setBorderWidth:borderWidth];
      [borderLayer setBorderColor:[color CGColor]];
  
      [self.layer addSublayer:borderLayer];
  }
  </code></pre>
  
  <p>This works for all the <code>UIView</code>'s that have layer's property <code>masksToBounds</code> or view's property <code>clipsToBounds</code> set to <code>NO</code>. But this disables the functionality to use cornerRadius property, which I want to have.</p>
  
  <p><strong>So my second attempt was to use <code>UIView</code>'s, since obviously I must go out of the view I want to stroke (the same logic could be applied to layer and superlayer relationship I assume).</strong></p>
  
  <p>I came up with this solution:</p>
  
  <pre><code>- (void)strokeViewWithColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
  {
      UIView* superview = self.superview;
  
      //
      // Fix frame
      //
  
      CGRect frame = self.frame;
      frame.origin.x -= borderWidth;
      frame.origin.y -= borderWidth;
      frame.size.width += (2 * borderWidth);
      frame.size.height += (2 * borderWidth);
  
      UIView* strokeView = [[UIView alloc] initWithFrame:frame];
      strokeView.layer.borderWidth = borderWidth;
      strokeView.layer.cornerRadius = self.layer.cornerRadius + borderWidth;
      strokeView.layer.borderColor = [color CGColor];
  
      [superview insertSubview:strokeView aboveSubview:self];
  }
  </code></pre>
  
  <p>This works, but the problem would be with auto layout. Because the <code>strokeView</code> has no auto layout constraints, it probably would not work correctly when constraints are added.</p>
  
  <p>That is why I am looking for any other solutions.</p>
  
  <p><strong>What would be the best way to solve this problem? Should I write some code to handle the auto layout issue on second part of the code? Or should I approach the problem from different perspective? If so, how?</strong> </p>
  
  <p><em>I'm mostly looking for suggestions on how to approach the problem.</em></p>
  
  <p>Thanks for help!</p>