IOS: drawing line appears behind tabs

I have a simple UIView in drawRect: I add a UIImageView as a subtitle, and then try to draw a line on top of this subview. But, the line draws on the image.

How can I make the drawing context subordinate to draw on top of it?

CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 10.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextBeginPath(context); CGContextMoveToPoint(context, 0, 0); CGContextAddCurveToPoint(context,125,150,175,150,200,100); CGContextAddCurveToPoint(context,225,50,275,75,300,200); CGContextStrokePath(context); 
+4
source share
2 answers

Make another custom view that has a single line drawing job. Add it as a preview with the same frame as ImageView, and call the bringSubviewToFront command to make sure it is ahead. You may need to set some attributes in your user view, for example opaque = NO , and set the background color to clear ( [UIColor colorWithWhite:0.0 alpha:0.0] ).

By the way, do not add any subzones to drawRect. drawRect should just draw, not change presentation attributes or hierarchy. You should add an ImageView and a custom line drawing view somewhere else, perhaps in init. For instance:

 @implementation MyView -(id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { imageView = ... // however you create your image view [self addSubview:imageView]; lineView = [[MyLineView alloc] initWithFrame:imageView.frame]; [self addSubview:lineView]; [self bringSubviewToFront:lineView]; } return self; } ... @end @implementation MyLineView -(void)drawRect:(CGRect)rect { // your drawing code // remember the coordinate system now has (0, 0) at the top left corner of the // image view, so adjust your drawing code accordingly } @end 
+7
source

Views are reversed. If you want something to appear in subview, subview should draw it.

+3
source

All Articles