ios - where to create autolayout constraints for subclassed uitableviewcell? -
i trying use autolayout uitableviewcell subclass creating , appreciate advice on method put layout constraint creation code. have been searching around , information find talks adding constraints after adding subviews in viewdidload method. far know viewdidload isn't option uitableviewcell subclass.
i using interface builder create custom cell , dynamically allocating in code. nothing special there... subclass uitableviewcell can add custom uiview cell. again, nothing particularly earth shattering... difficulty comes when try position custom uiview respect label added cell in interface builder.
this code create custom uiview , add cell's content view:
- (id)initwithcoder:(nscoder *)decoder { if ((self = [super initwithcoder:decoder])) { [self initself]; } return self; } - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { if ((self = [super initwithstyle:style reuseidentifier:reuseidentifier])) { [self initself]; } return self; } - (void) initself { // initialization code _badgelabel = @""; if (!_custombadge) { _custombadge = [custombadge custombadgewithstring:self.badgelabel]; } // hide badge until needed self.custombadge.hidden = yes; // add badge cell view hierarchy [self.custombadge settranslatesautoresizingmaskintoconstraints:no]; [self.custombadge setcontenthuggingpriority:uilayoutprioritydefaulthigh foraxis:uilayoutconstraintaxishorizontal]; [self.custombadge setcontenthuggingpriority:uilayoutprioritydefaulthigh foraxis:uilayoutconstraintaxisvertical]; [self.contentview addsubview:self.custombadge]; }
if put constraint code @ end of initself nothing happens. position of _custombadge stays @ its' default. when put constraint code in layoutsubviews positioning applied; pretty sure wrong place. here's code:
- (void) layoutsubviews { [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.custombadge attribute:nslayoutattributeleft relatedby:nslayoutrelationequal toitem:self.competence attribute:nslayoutattributeright multiplier:1.0 constant:-14.0]]; [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.custombadge attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.competence attribute:nslayoutattributetop multiplier:1.0 constant:0.0]]; [super layoutsubviews]; }
can tell me code should go? surely creating duplicate constraints every time layout happens.
thanks
you need update constraints like:
-(void)updateconstraints{ // add constraints if not there [super updateconstraints]; }
after adding views superview need call [self setneedsupdateconstraints]
start using them. doing rendering runtime call updateconstraints
@ right time.
Comments
Post a Comment