ios - Updating width of uilabel when subview not -
i have uilabel
in uitableviewcell
neighboring subview uicontrolswitch
. when uicontrolswitch
hidden, uilabel's
width expected grow. below custom class implementation:
- (void)setbounds:(cgrect)bounds { [super setbounds:bounds]; self.contentview.frame = self.bounds; } - (void)layoutsubviews { [super layoutsubviews]; [self.contentview updateconstraintsifneeded]; [self.contentview layoutifneeded]; self.numberlabel.preferredmaxlayoutwidth = cgrectgetwidth(self.numberlabel.frame); self.quotelabel.preferredmaxlayoutwidth = cgrectgetwidth(self.quotelabel.frame); } @end
i have uploaded sample code explain constraints on storyboard.
the uilabel's
width not changing. if set trailing space between uilabel
, uicontrolswitch
, control partially hidden beyond screen bounds. hence constraint uilabel
has trailing space superview instead of uicontrolswitch
.
when hide view, still takes part in layout process. hiding uiswitch not cause change.
so either have add/remove constraints, remove uiswitch containing view or change how doing constraints.
option 1: instead of hide/show remove/add.
if put uiswitch in own view, can pin uilabel switches containing view. instead of hiding uiswitch, remove containing view. result containing view collapses , uilabel stretch long label pinned switches view , switches view edge.
instead of show, add uilabel container view. view stretch , uilabel shrink.
option 2: use simple trailing constraint uilabel.
make uilabel have trailing constraint superview , ctrl drag constraint code can set value. or add manually if doing.
do not pin uilabel uiswitch @ all. pin uilabel right h§and side has required position.
so uilabel pinned right edge offset of choosing. if set small, uilabel over/under switch. if set width of switch plus left , right margin appear stretch switch.
in code, can find width of uiswitch looking @ frame.
1) when uiswitch visible, set trailing pin constraint of uilabel margin want + width of uiswitch + margin left of uiswitch want.
2) when uiswitch hidden, set trailing pin constraint of uilabel margin want superview.
this should cause uilabel extend switch when not hidden, or edge when switch hidden.
Comments
Post a Comment