ios - How do I add a constraint such that a view is as wide as the widest of two other views? -
for example, labels a, b , c. label should have width of whichever wider, b or c.
visually, along lines of
@"h:[viewa(==max(viewb,viewc))]"
if want ensure viewa
simultaneously @ least wide both viewb
, viewc
, use 2 separate vfl statements, @"h:[viewa(>=viewb)]"
, @"h:[viewa(>=viewc)]"
.
if want ensure not wider maximum width of viewb
and/or viewc
, define optional constraint (i.e. lower priority uilayoutpriorityrequired
) width of viewa
equal 1 of them, e.g.:
nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:viewa attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:viewb attribute:nslayoutattributewidth multiplier:1.0 constant:0.0]; constraint.priority = uilayoutprioritydefaultlow; [viewa.superview addconstraint:constraint];
according documentation priority
:
if constraint 'a == b' optional, constraint-based layout system attempt minimize 'abs(a-b)'.
thus, if viewb
larger viewc
, optional constraint satisfied , viewa
wide viewb
. if viewb
smaller viewc
, constraint system satisfy required @"h:[viewa(>=viewc)]"
constraint, minimize abs(a-b)
, making viewa
same width viewc
.
in practice, don't need optional viewa==viewb
constraint, if want ensure viewa
won't wider both viewb
, viewc
, add final optional constraint.
Comments
Post a Comment