Outlining a UIView

Posted by admin on Jun 7, 2009 in iPod / iPhone, Mac OS X |

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];

Comments are closed.

Copyright © 2017 Standalone Code LLC All rights reserved. Theme by Laptop Geek.