0

I have a UIViewController which has a UIImageView. I wanted to draw a line on top of the UIImageView. I know that in order to do this I have to override the drawRect in a UIView. Is there an alternative to this?

2
  • I answered below, but why would you not want to override drawRect? Commented Feb 22, 2012 at 20:40
  • because it would be annoying to just have a UIImage class with a drawRect on it? Commented Feb 22, 2012 at 22:07

2 Answers 2

1

Something like this:

CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 1.0f); CGContextMoveToPoint(context, 0, 0); CGContextAddLineToPoint(context, view.frame.size.width, view.frame.size.height); CGContextStrokePath(context); 
Sign up to request clarification or add additional context in comments.

1 Comment

@adit, I don't know what is your imageview name. It's just a common example.
0

While overriding drawRect may be the best solution, there are always alternatives.

If the line is horizontal or vertical, you could create a UIView with the frame of the line you need and set its background color.

You could overlay an image of the line, rotated/resized as needed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.