ios - Center a dynamically created UIView over a UILabel in the storyboard not working -
i have uilabel
placed on storyboard autolayout. (constraints include height, width , center horizontally, , vertical spacing top).
i have uiview
makes circle drawrect
method using uibeizerpath
.
nothing fancy far.
for simplicity sake using hard coded numbers illustrate problem.
now want circleview placed on uilabel
, centering label in circle.
however, can not line correctly. anchor points or messing up.
i tried setting circleview's centerpoint labels center point. no luck.
i tried setting circleviews x location of label minus width divided two. no luck.
the closest can y coordinate. use label.center.y - 52.5 (half of radius).
cv = [[circleview alloc] initwithframe:cgrectmake(whathere.x, whathere.y, 155, 155)]; cv.radius = 75; cv.strokewidth = 5;
the radius of circle 75. width/height of circleview 155 because stroke of circle 5. (radius x 2) + 5, give me view see circle.
the dark background view of ios simulator. have added background colors each of elements distinguish size.
through magic of photoshop here trying accomplish:
then you're doing wrong... use constraints !
here storyboard label constraints
here viewcontroller
@interface viewcontroller () @property (nonatomic, weak) iboutlet uilabel *centeredlabel; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. circleview *circleview = [circleview new]; [self.view addsubview:circleview]; circleview.translatesautoresizingmaskintoconstraints = no; nslayoutconstraint *circlecenterx = [nslayoutconstraint constraintwithitem:circleview attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self.centeredlabel attribute:nslayoutattributecenterx multiplier:1 constant:0]; nslayoutconstraint *circlecentery = [nslayoutconstraint constraintwithitem:circleview attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:self.centeredlabel attribute:nslayoutattributecentery multiplier:1 constant:0]; cgfloat circlediameter = 155; nslayoutconstraint *circlewidth = [nslayoutconstraint constraintwithitem:circleview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:circlediameter]; nslayoutconstraint *circleheight = [nslayoutconstraint constraintwithitem:circleview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:circleview attribute:nslayoutattributewidth multiplier:1 constant:0]; [self.view addconstraints:@[circlecenterx, circlecentery, circlewidth, circleheight]]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
here's circleview
@implementation circleview - (instancetype)init{ self = [super init]; if (self) { self.backgroundcolor = [uicolor clearcolor]; } return self; } - (void)drawrect:(cgrect)rect { cgcontextref ctx = uigraphicsgetcurrentcontext(); uicolor *bluetransparent = [[uicolor bluecolor] colorwithalphacomponent:0.4]; [bluetransparent setfill]; cgcontextfillrect(ctx, self.bounds); uicolor *circlecolor = [uicolor greencolor]; [circlecolor setstroke]; cgcontextsetlinewidth(ctx, 6); cgcontextstrokeellipseinrect(ctx, self.bounds); } @end
and here's result
piece of cake ! ;)
Comments
Post a Comment