ios - Cant drawing rectangles over image in custom UIView -
i have scene of storyboard uiviewcontroller. inside scene have uiimageview contains background image, uibutton, , uiview.
this uiview have overrided drawrect method with:
- (void)drawrect:(cgrect)rect { [super drawrect:rect]; [self setneedsdisplay]; cgfloat height = self.bounds.size.height; cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextclearrect(context, rect); cgcontextsetfillcolorwithcolor(context, [uicolor graycolor].cgcolor); cgfloat barwidth = 30; int count = 0; nsarray *values = [nsarray arraywithobjects:@1, @0.5, nil]; (nsnumber *num in values) { cgfloat x = count * (barwidth + 10); cgrect barrect = cgrectmake(x, height - ([num floatvalue] * height), barwidth, [num floatvalue] * height); cgcontextaddrect(context, barrect); count++; } cgcontextfillpath(context); }
my question is: how can set image background of custom uiview , drawing rectangles on it?
regards
let's named uiview subclass mycustomview. when adding uiview interface-builder (xib or storyboard), must explicitly set custom class of uiview interface builder mycustomview (like in answer).
another problem might occur order of views. 1 on top?
adding custom view code way it.
your drawing code seems ok. here code drawrect
draws image in background (i tweaked little bit):
- (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextclearrect(context, rect); // here draw image before drawing rectangles uiimage* img = [uiimage imagenamed:@"img.jpg"]; [img drawinrect:rect]; cgfloat height = self.bounds.size.height; cgfloat barwidth = 30; cgcontextsetfillcolorwithcolor(context, [[uicolor graycolor] cgcolor]); int count = 0; nsarray *values = [nsarray arraywithobjects:@1, @0.5, nil]; (nsnumber *num in values) { cgfloat x = count * (barwidth + 10); cgrect barrect = cgrectmake(x, height - ([num floatvalue] * height), barwidth, [num floatvalue] * height); // drawing rectangles on image cgcontextfillrect(context, barrect); count++; } }
Comments
Post a Comment