Outlining a UIView

I recently was having some difficulty with some drawing in a UIView and decided that outlining the bounds of the view itself would help me to debug the problem.  (It did.  Instantly.)  All you have to do is to stroke the bounds rectangle of the view you’re in (this assumes you have already subclassed UIView for your custom view).  Add the following code to your drawRect: method:

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
  CGContextStrokeRect(context, self.bounds);

Update 17-Feb-2010:

This can be done without subclassing (or with, if you prefer) very easily. Simply access the view’s CA layer and set the border on it:

[[aView layer] setBorderWidth:1.0];