tags:

views:

52

answers:

2

basic question, but I'm unsure. Not looking for code as an answer.

I want to draw 4 short lines 1px lines on a view. What is the best way to approach this task? Options:-

  • Load an image of the line, then create 4 UIImageViews with it.
  • Create my own subclass of a UIView that draws a line in the draw rect method.
  • Draw elsewhere on another view, another UIImageView that has an UIImage inside it (is this possible?)
  • Another way?

Thanks Ross

+2  A: 

Simplest way is to create a UIView subclass and perform your drawing in drawRect:. See the CoreGraphics guide for details on how to draw a line.

rpetrich
+1  A: 

Quick and dirty way to achieve this is to just create a UIView and set it's height (or width, depending on its orientation) to 1px, and then set a background colour and slap it onto your view as a subview.

Jasarien
nice. I know really this doesn't matter, but for science's sake: will it just require as much resources as using `drawRect:`?
Ross
Draw rect may be more efficient, I don't know really. I imagine that a 1px high view with a black background would consume the same amount of processing as a view that drew a 1px black line. Though depending on the view, if it was 10px high and draw the line in the center, and the rest of the view was white, I imagine that would take more processing since there's a bigger area to draw. Again, I'm just guessing.
Jasarien
Each view has a fixed rendering cost and additional costs in relation to the size/transform/other properties. If you only have one line, the single view with 1px height or width will likely be quicker, but at some point multiple lines in a single view will outpace it. If performance becomes an issue, profile to see which works best
rpetrich