ios - How to change the value of 'mutiplier' property of NSLayoutConstarint in iOS8 -
i use adaptive layout features designing app. take iboutlet
of "aspect ratio" constraint . want change value of aspect ratio value double of current value. problem "constraint
" property can set code, "multiplier
" property read property. aspect ratio change, "multipier" value change necessary . how this?.
@property (retain, nonatomic) iboutlet nslayoutconstraint *leftimagewidthaspectratio;
in code
nslog(@"cell.leftimagewidthaspectratio:%@ : %lf %lf",cell.leftimagewidthaspectratio, cell.leftimagewidthaspectratio.constant,cell.leftimagewidthaspectratio.multiplier);
results
cell.leftimagewidthaspectratio:<nslayoutconstraint:0x7c9f2ed0 uiview:0x7c9f2030.width == 2*rifeedthumbimageview:0x7c9f2c90.width> : 0.000000 2.000000
you’re right—changing multiplier on existing constraint isn’t supported. constant
exception, not rule. docs:
unlike other properties, constant can modified after constraint creation. setting constant on existing constraint performs better removing constraint , adding new 1 that's old except has different constant.
what need what’s described @ end there: replace existing constraint identical-but-with-a-different-multiplier one. should work:
nslayoutconstraint *oldconstraint = cell.leftimagewidthaspectratio; cgfloat newmultiplier = 4; // or whatever nslayoutconstraint *newconstraint = [nslayoutconstraint constraintwithitem:oldconstraint.firstitem attribute:oldconstraint.firstattribute relatedby:oldconstraint.relation toitem:oldconstraint.seconditem attribute:oldconstraint.secondattribute multiplier:newmultiplier constant:oldconstraint.constant]; newconstraint.priority = oldconstraint.priority; [cell removeconstraint:oldconstraint]; [cell addconstraint:newconstraint];
note cell
may wrong view this—it depends ib decides put original constraint. if doesn’t work, dig through superviews of constrained views (you can check constraints have constraints
property) until find it’s ending up.
Comments
Post a Comment